Question

I'm trying to start a small JSF application with no success. Tried it on Glassfish but I had the following error (Trying to use CDI on Glassfish 4 results in javax.el.PropertyNotFoundException: Target Unreachable, identifier 'indexMB' resolved to null).

So I tried to migrate the application to the TomEE 1.5.2, but although display no error in the console, the page does not load the JSF components, as you can see in the image below:

JSF not loade

Any help will be very useful.

Follow my setup and my files:

  • TomEE 1.5.2
  • Primefaces 3.5
  • commons-fileupload 1.3
  • commons-io 2.4

** index.xhtml

<html ...>

<f:loadBundle basename="i18n" var="bundle" />
<h:head>
    <title>#{bundle['index_title']}</title>
</h:head>
<h:body>
    #{bundle['index_appname']}
    <br />
    <h:form id="frmIndex">
        <p:panelGrid columns="2">
            <p:outputLabel for="user" value="#{bundle['lblUser']}" />
            <p:inputText id="user" value="#{indexMB.user}" />

            <p:outputLabel for="password" value="#{bundle['lblPassword']}" />
            <p:password id="password" value="#{indexMB.password}" />
        </p:panelGrid>
        <p:commandButton action="#{indexMB.loginTest}" value="#{bundle['btn_login']}" />
    </h:form> 
</h:body>

** IndexMB.java

@ManagedBean ("indexMB")
@RequestScoped
public class IndexMB {

private String password;
private String user;

public IndexMB() {
}

public String loginTest(){
   ...
}

// getters and setters
}

** web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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-app_3_0.xsd">
<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>
<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>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>

** faces-config.xml

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
    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_2.xsd">

<application>
    <locale-config>
        <default-locale>pt_BR</default-locale>
        <supported-locale>en</supported-locale>
        <supported-locale>fr</supported-locale>
    </locale-config>
</application>

** beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans 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/beans_1_0.xsd">
</beans>
Was it helpful?

Solution

the page does not load the JSF components

The page is loading the components, otherwise there won't be a <input type="text" /> that comes from the <p:inputText id="user" value="#{indexMB.user}" /> in your Facelets code (along with other components). The problem seems to be in your #{bundle['<whatever>']} configuration. Note that this problem is not related to GlassFish nor TomEE at all, just about the internationalization configuration.

For JSF 2.x I followed the explanation from this Q/A: https://stackoverflow.com/a/4830669/1065197 and the link that provides more info: Internationalization in JSF with UTF-8 encoded properties files. Based in that tutorial I made a test application. In short:

  1. Configure the bundle in your faces-config.xml

    <application>
        <locale-config>
            <default-locale>pt_BR</default-locale>
            <supported-locale>en</supported-locale>
            <supported-locale>fr</supported-locale>
        </locale-config>
        <resource-bundle>
                    <!--
                        Here should be the full name of the package and the 
                        name of the properties files with the i18n text.
                        Example (from a personal project):
                        edu.home.store.view.idioma.tienda
                        Where the properties files are:
                        edu.home.store.view.idioma.tienda_es.properties
                        edu.home.store.view.idioma.tienda_en.properties
                    -->
                    <!-- <base-name>edu.home.store.view.idioma.tienda</base-name> -->
                    <!-- assumming your file is directly posted in class folder -->
                    <base-name>i18n</base-name>
                    <!--
                        Name of the variable to use in Facelets files.
                    -->
            <var>bundle</var>
        </resource-bundle>
    </application>
    
  2. Create a @SessionScoped bean that will handle the locale to use in your pages.

    @ManagedBean
    @SessionScoped
    public class LocaleBean implements Serializable {
    
        private static final long serialVersionUID = 89794215646544L;
    
        private Locale locale;
    
        public LocaleBean() {
    
        }
    
        @PostConstruct
        public void init() {
            //give the default value here
            locale = new Locale("pt_BR");
            FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
        }
    
        public Locale getLocale() {
            return locale;
        }
    
        public String getLenguaje() {
            return locale.getLanguage();
        }
    
        public void setLenguaje(String lenguaje) {
            locale = new Locale(lenguaje);
            FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
        }
    }
    
  3. Wrap all the content of your view using <f:view> and define the language to use. Preferably, this should go in the main template. Example:

    <f:view locale="#{localeBean.locale}">
        <!-- Your Facelets code goes here... -->
    </f:view>
    

Apart from this problem, I highly recommend you to change the obviously autogenerated Faces Servlet mapping configuration from /faces/* to *.xhtml:

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

More info:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top