CDI with JSF 2.3 on Open Liberty

created onJanuary 21, 2022

@FacesConverter does not work with the JSF 2.3 feature of Openliberty with a default config, at least not if the converter is generic. CDI and JSF 2.3 don’t work with the default config.

Error msg and stack trace from mvn:

[INFO] [ERROR ] SRVE0271E: Uncaught init() exception created by servlet [facesServlet] in application [testapp]: java.lang.IllegalStateException: No Factories configured for this Application. This happens if the faces-initialization does not work at all - make sure that you properly include all configuration settings necessary for a basic faces application and that all the necessary libs are included. Also check the logging output of your web application and your container for any exceptions! [INFO] If you did that and find nothing, the mistake might be due to the fact that you use some special web-containers which do not support registering context-listeners via TLD files and a context listener is not setup in your web.xml. [INFO] A typical config looks like this; [INFO] <listener> [INFO] <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class> [INFO] </listener> [INFO] [INFO] at javax.faces.FactoryFinder._getFactory(FactoryFinder.java:310) [INFO] at [internal classes] [INFO] [INFO] [ERROR ] SRVE0276E: Error while initializing Servlet [facesServlet]: javax.servlet.ServletException: SRVE0207E: Uncaught initialization exception created by servlet [INFO] at com.ibm.ws.webcontainer.servlet.ServletWrapper.init(ServletWrapper.java:368) [INFO] at [internal classes] [INFO] Caused by: java.lang.IllegalStateException: No Factories configured for this Application. This happens if the faces-initialization does not work at all - make sure that you properly include all configuration settings necessary for a basic faces application and that all the necessary libs are included. Also check the logging output of your web application and your container for any exceptions! [INFO] If you did that and find nothing, the mistake might be due to the fact that you use some special web-containers which do not support registering context-listeners via TLD files and a context listener is not setup in your web.xml. [INFO] A typical config looks like this; [INFO] <listener> [INFO] <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class> [INFO] </listener> [INFO] [INFO] at javax.faces.FactoryFinder._getFactory(FactoryFinder.java:310) [INFO] ... 1 more [INFO]

configuration

to configure openliberty for CDI and JSF 2.3:

web.xml

location:

<context-param> <param-name>javax.faces.validator.ENABLE_VALIDATE_WHOLE_BEAN</param-name> <param-value>true</param-value> </context-param> <context-param> <param-name>javax.faces.ENABLE_CDI_RESOLVER_CHAIN</param-name> <param-value>true</param-value> </context-param>

beans.xml

location:

<?xml version="1.0" encoding="UTF-8"?> <beans 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_2_0.xsd" bean-discovery-mode="all" version="2.0"> </beans>

faces-config.xml

location:

<?xml version='1.0' encoding='UTF-8'?> <faces-config 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/web-facesconfig_2_3.xsd" version="2.3"> </faces-config>

faces configuration

is not enough to make CDI work with JSF

class ConfigurationBean, location somewhere in

package de.knusperfisch.dsgvo.app; import javax.enterprise.context.ApplicationScoped; import javax.faces.annotation.FacesConfig; import static javax.faces.annotation.FacesConfig.Version.JSF_2_3; @FacesConfig( // Activates CDI build-in beans version = JSF_2_3 ) @ApplicationScoped public class ConfigurationBean { }

Caution: If bean discovery mode is switched from “all” to “annotated”, every JSF backing bean must be declared with @FacesConfig, which sucks.

using @FacesConverter

When using @FacesConverter, use it together with ‘forClass’, not using the parameter ‘forClass’ (or its alternative) will again result in the above err msg / stack trace.

reference

hantsyblog Activating CDI in JSF 2.3