Search code examples
javajsfwebspherecdi

JSF CDI example with liberty server does not work


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>

Solution

  • WebSphere Liberty provides its own JSF features - jsf-2.0, jsf-2.2, and jsf-2.3 - which allow applications to make use of the corresponding MyFaces implementations. These features have integration with the server-provided CDI features, such that enabling jsf-2.2 along with cdi-1.2 will allow this type of app to work without any additional included CDI or JSF libraries.

    Given the information you've provided, your most straightforward option here will be to remove the remove the libraries you've listed (jsf-*.jar, java*.jar) from your application and enable the jsf-2.2 and cdi-1.2 features.

    If you are intentionally bundling a different JSF implementation in your application, CDI integration will not work out-of-the-box. If you need to use CDI with an application-bundled JSF implementation, you can make use of the jsfContainer-2.2 or jsfContainer-2.3 features. See https://www.ibm.com/support/knowledgecenter/en/SS7K4U_liberty/com.ibm.websphere.liberty.autogen.nd.doc/ae/rwlp_feature_jsfContainer-2.2.html