0

I want to implement simple JSF page based on this tutorial with JSF 2.2

faces-config.xml

<?xml version="1.0"?>
<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_2.xsd"
              version="2.2">
    <application>
        <locale-config>
            <default-locale>en</default-locale>
            <supported-locale>es</supported-locale>
        </locale-config>
        <resource-bundle>
            <base-name>com.web.common.internationalization.text</base-name>
            <var>text</var>
        </resource-bundle>
    </application>
</faces-config>

index.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="#{index.language}"
      xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets">
    <f:view locale="#{index.locale}">
        <h:head>

        </h:head>
        <h:body>

            <h:form>
                <h:selectOneMenu value="#{index.language}" onchange="submit()">
                    <f:selectItem itemValue="en" itemLabel="English" />
                    <f:selectItem itemValue="es" itemLabel="Spanish" />
                </h:selectOneMenu>
            </h:form>

            <address class="top-address">
                <span><h:outputText value="#{text['customer.support']}" /></span>&nbsp;
            </address>

        </h:body>
    </f:view>
</html>

CDI Bean

 import java.io.Serializable;
import java.util.Locale;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;

@Named
@SessionScoped
public class Index implements Serializable
{
    private Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();

    public Locale getLocale()
    {
        return locale;
    }

    public String getLanguage()
    {
        return locale.getLanguage();
    }

    public void setLanguage(String language)
    {
        this.locale = new Locale(language);
    }
}

I added here Text Java Class from the tutorial and the properties files.

enter image description here

But for some reason when I open the web page I get error code 500. Do you have any idea where I'm wrong?

Error stack:

javax.el.PropertyNotFoundException: /index.xhtml @15,80 value="#{index.language}": Target Unreachable, identifier 'index' resolved to null
    at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100)
    at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:95)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectOneValue(MenuRenderer.java:201)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:318)
    at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1046)
    at javax.faces.component.UIInput.validate(UIInput.java:976)
    at javax.faces.component.UIInput.executeValidate(UIInput.java:1249)
    at javax.faces.component.UIInput.processValidators(UIInput.java:712)
    at javax.faces.component.UIForm.processValidators(UIForm.java:253)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1261)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1261)
    at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1195)
    at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javax.el.PropertyNotFoundException: Target Unreachable, identifier 'index' resolved to null
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

1 Answers1

2

You got the wrong import - the @SessionScoped is from the faces package, it should be the one from the javax.enterprise.context package.

Or you can switch @Named to @javax.faces.bean.ManagedBean, then the @SessionScoped will match (it will no longer be a CDI bean though).

fdreger
  • 12,264
  • 1
  • 36
  • 42
  • Referring to the 'duplicate' is an option to ;-) : http://stackoverflow.com/questions/11986847/java-ee-6-javax-annotation-managedbean-vs-javax-inject-named-vs-javax-faces – Kukeltje Nov 03 '15 at 13:20
  • Now I get NPE at this line `private Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();` – Peter Penzov Nov 03 '15 at 13:21
  • @Kukeltje: if everyone did it, there would be no StackOverflow as we know it :-) – fdreger Nov 03 '15 at 13:27
  • I found this http://stackoverflow.com/questions/30653945/facescontextgetviewroot-returns-null-while-setting-fview-locale-for-first For now it solved the problem. – Peter Penzov Nov 03 '15 at 13:32
  • By the way when I switch the JSF pages into the war package(all other beans are in ViewScope) are these properties going to be preserved? – Peter Penzov Nov 03 '15 at 13:33
  • @PeterPenzov What do you mean by a "property"? Generally packaging doesn't change anything. – fdreger Nov 03 '15 at 13:49
  • I mean if I select Spanish on the fist page and I open several other pages and I return back to the fist one is it going to remember that I had selected Spanish? – Peter Penzov Nov 03 '15 at 13:54
  • As long as the session lasts, yes. – fdreger Nov 05 '15 at 13:28