I have build an easy example with JSF+CDI+Liberty Server, but I always get the message that the target of my bean is not available and I don't understand why.
I have implemented the following libraries:
- javaee-api-7.0.jar
- javax.servlet-api-3.1.0.jar
- jsf-api-2.2.12.jar
- jsf-impl-2.2.2.jar
I use Java 1.8 with WebShere Application Server 18.0.0.2.
If i run the application the application I can see the input fields, but when i enter something and click on the send button, I always get the following error:
SRVE0777E: Exception thrown by application class 'javax.faces.webapp.FacesServlet.service:659' javax.servlet.ServletException: /pages/index.xhtml @12,72 value="#{userBean.firstName}": Target Unreachable, identifier 'userBean' resolved to null
Is it possible that there is something wrong with the versions?
UserBean.java
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
@Named
@RequestScoped
public class UserBean {
private String firstName;
private String lastName;
//Getter and Setter
}
index.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:body>
<h1>JSF 2.2 CDI Managed Bean</h1>
<h:form>
<h:panelGrid columns="2">
<h:outputLabel value="First name" for="firstName"/>
<h:inputText id="firstName" value="#{userBean.firstName}"/>
<h:outputLabel value="Last name" for="lastName"/>
<h:inputText id="lastName" value="#{userBean.lastName}"/>
<h:commandButton action="result" value="send"/>
</h:panelGrid>
</h:form>
</h:body>
</html>
result.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:body>
<h1>JSF 2.2 CDI Managed Bean</h1>
First name: #{userBean.firstName}
Last name: #{userBean.lastName}
</h:body>
</html>
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans bean-discovery-mode="all" version="1.1"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd">
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>JavaServerFaces</display-name>
<!-- Welcome page -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
faces-config.xml
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
</faces-config>