Using CDI (Context & Dependency Injection) beans support instead of managed Beans

I went to use CDI beans as beans support instead of JSF managed beans.

So, I decided to create a small example to understand how it works for @RequestScopedBean :

- instead of using @ManagedBean ("beanName") , I use @Named ("beanName")

- instead of using javax.faces.bean.RequestScopped I use javax.enterprise.context.RequestScoped;

The demo program is very simple: I have a field and a submit button, when the user enters something, and the page refreshes, the entered value is no longer displayed (does it continue while the request lasts the same?). I think everything was doing fine, but I get an exception that says:

WARNING: StandardWrapperValve [Persons Servlet]: PWC1406: Servlet.service () for servlet exception javax.el.PropertyNotFoundException: /index.xhtml @ 19.47 value = "# {cdiBean.passedValue}": Target Unreachable, identifier 'cdidian' id ' allowed null

This is what my program looks like:

index.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <h:head> <title>RequestScope demo CDI(Component Dependency Injection)</title> </h:head> <h:body> <h:form> <h3>RequestScope demo CDI(Component Dependency Injection)</h3> <h:inputText value="#{cdiBean.passedValue}"/> <br/> <h:commandButton value="submit" action="index"/> </h:form> </h:body> </html> 

DemoBB.java

 package backingbeans; import javax.enterprise.context.RequestScoped; import javax.inject.Named; @Named("cdiBean")//The Named anotation indicates that this is a CDI bean @RequestScoped//If we use CDI beans the @RequestScoped annotation must come from: javax.enterprise.context.RequestScoped; public class DemoBB { //This value will be saved on the session only until the server responds to the request private String passedValue; public String getPassedValue() { return passedValue; } public void setPassedValue(String passedValue) { this.passedValue = passedValue; } } 

β€œWhere is my mistake?”

- What is the advantage of using this approach? I still do not understand this.

+4
source share
1 answer

Do you have an empty beans.xml along with your web.xml ? I think it's imperative to be there.

Read section 15.6 here . Quote from this:

CDI does not define any special deployment archives. You can pack beans in a JAR, EJB-JAR, or WARs-any deployment location in a CLASSPATH application. However, the archive must be a bean archive. This means that each archive containing beans must specify a file named beans.xml in the META-INF directory for the class path or WEB-INF for the web root directory (for WAR). The file may be empty. beans are deployed in archives that the beans.xml file will not be available for use in the application.

In an enclosed EJB container, beans can be deployed anywhere in which EJBs can be deployed. Once again, each location should contain a beans.xml file.

+3
source

All Articles