1

I started to test JSF 2.3 a short time ago. But I can't get one of the most important features to work. The use of ManagedBeans. I tried a lot, using different servlet containers (Tomcat 8&9, Jetty 9.2).But nothing helped. Hope somebody can see my failure in the resources. It's frustrating. I debugged but the bean is never reached. The primefaces component works fine(the primefaces lib is not the reason). But I never get bean data. PS. I'm using myfaces but with mojarra I've got the same problem.

my bean:

import javax.enterprise.context.RequestScoped;
import javax.faces.annotation.FacesConfig;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import java.io.Serializable;

@Named(value = "sampleBean")
@RequestScoped
@FacesConfig(version = FacesConfig.Version.JSF_2_3)
public class SampleBean implements Serializable {
    private String specTitle;
    private String specVersion;
    private String implTitle;
    private String implVersion;


    public SampleBean() {
        Package facesPackage = FacesContext.class.getPackage();
        specVersion = facesPackage.getSpecificationVersion();
        specTitle = facesPackage.getSpecificationTitle();
        implTitle = facesPackage.getImplementationTitle();
        implVersion = facesPackage.getImplementationVersion();
    }

    public String info() {
        return "hello from sampleBean!";
    }

    public String getSpecTitle() {
        return specTitle;
    }

    public String getSpecVersion() {
        return specVersion;
    }

    public String getImplTitle() {
        return implTitle;
    }

    public String getImplVersion() {
        return implVersion;
    }

}

my configuration bean:

import javax.faces.annotation.FacesConfig;

@FacesConfig(version = FacesConfig.Version.JSF_2_3)
public class ConfigurationBean {
}

my facelet:

<?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"
      xmlns:p="http://primefaces.org/ui">
<h:head>
    <title>Hello JSF 2.3</title>
    <style>
        .col-1 { text-align: right; }
        .col-2 { font-weight: bold; }
    </style>
</h:head>
<h:body>

<div style="border-style: dashed" class="ui-g">
<p:calendar mode="inline"/>
</div>
<div style="border-style: double">
<h3>Hello from JSF 2.3!</h3>

<h:panelGrid columns="2" columnClasses="col-1, col-2">

    <h:outputText value="Specification:"/>
    <h:outputText value="#{sampleBean.specTitle}"/>

    <h:outputText value="Specification version:"/>
    <h:outputText value="#{sampleBean.specVersion}"/>

    <h:outputText value="Implementation:"/>
    <h:outputText value="#{sampleBean.implTitle}"/>

    <h:outputText value="Implementation version:"/>
    <h:outputText value="#{sampleBean.implVersion}"/>
</h:panelGrid>

<p>CDI injection support: <b>#{sampleBean.facesContextValue}</b></p>
<p>Running on: <b>#{application.serverInfo}</b></p>
</div>

my web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app 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-app_3_1.xsd"
             version="3.1"><!-- Use web-app_4_0.xsd, version=4.0 after update to Java EE 8 -->

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.ENABLE_CDI_RESOLVER_CHAIN</param-name>
        <param-value>truet</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.ENABLE_WEBSOCKET_ENDPOINT</param-name>
        <param-value>true</param-value>
    </context-param>

    <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.FACELETS_REFRESH_PERIOD</param-name>
        <param-value>0</param-value> <!-- No Cache  -->
    </context-param>

    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>

</web-app>

my build.gradle:

plugins {
    id 'war'
    id 'org.gretty' version '2.2.0'
}

group 'de.danri'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
    jcenter()
}

gretty{
    //servletContainer="tomcat8"
    //servletContainer="tomcat9"
}

dependencies {
    // https://mvnrepository.com/artifact/org.apache.myfaces.core/myfaces-impl
    compile group: 'org.apache.myfaces.core', name: 'myfaces-impl', version: '2.3.1'
    // https://mvnrepository.com/artifact/org.apache.myfaces.core/myfaces-api
    compile group: 'org.apache.myfaces.core', name: 'myfaces-api', version: '2.3.1'
    // https://mvnrepository.com/artifact/org.primefaces/primefaces
    compile group: 'org.primefaces', name: 'primefaces', version: '6.2'
    // https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api
    compile group: 'javax.servlet.jsp', name: 'jsp-api', version: '2.2.1-b03'
    // https://mvnrepository.com/artifact/javax.enterprise/cdi-api
    compile group: 'javax.enterprise', name: 'cdi-api', version: '2.0.SP1'
    // https://mvnrepository.com/artifact/javax.websocket/javax.websocket-api
    compile group: 'javax.websocket', name: 'javax.websocket-api', version: '1.1'
    // https://mvnrepository.com/artifact/javax.servlet/jstl
    compile group: 'javax.servlet', name: 'jstl', version: '1.2'
    // https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api
    compile group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
    // https://mvnrepository.com/artifact/javax.validation/validation-api
    compile group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'

    testCompile group: 'junit', name: 'junit', version: '4.12'
}

beans.xml:

<?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_1_1.xsd"
       version="1.1" bean-discovery-mode="all">
</beans>

faces-config.xml:

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.3"
              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">
</faces-config>
  • 2
    See this balusc CDI integration with tomcat: http://balusc.omnifaces.org/2013/10/how-to-install-cdi-in-tomcat.html Your bean is a CDI managed bean, not a JSF managed bean, and hence CDI must take care of putting it in the jsf context – maress Aug 31 '18 at 08:04

1 Answers1

0

Thank your maress. Good adress! Solution: I added the /META-INF/context.xml

<Context>
    <Resource name="BeanManager"
              auth="Container"
              type="javax.enterprise.inject.spi.BeanManager"
              factory="org.jboss.weld.resources.ManagerObjectFactory" />
</Context>

I added weld-servlet shaded to my dependencies:

...
compile group: 'org.jboss.weld.servlet', name: 'weld-servlet-shaded', version: '3.0.5.Final'
...

Finally I used a local tomcat to run the artifact and not the gradle 'gretty' plugin. It seems the plugin has problems with cdi.