0

I'm trying to add localization support in my jsf app. But when I change locale, in goes wrong page address. If current page address is http://localhost:8092/ (first default page), after locate change the redirected page address is http://web-inf/views/authors.xhtml. Of course this page is not available. This is my web.xml:

<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">

    <!-- Welcome page -->
    <welcome-file-list>
        <welcome-file>/WEB-INF/views/authors.xhtml</welcome-file>
    </welcome-file-list>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <listener>
        <listener-class>
            org.springframework.web.context.request.RequestContextListener
        </listener-class>
    </listener>

    <!-- JSF mapping -->
    <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>/WEB-INF/views/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.faces</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
</web-app>

This is my JSF page authors.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">

<h:head>
    <title>JSF 2.0 Hello World</title>
</h:head>
<h:body>

    <h1>JSF 2 internationalization example</h1>

    <h:form>

        <h2>
            <h:outputText value="#{msg['welcome.jsf']}" />
        </h2>

        <h:panelGrid columns="2">

            Language :
            <h:selectOneMenu value="#{language.localeCode}" onchange="submit()"
                             valueChangeListener="#{language.countryLocaleCodeChanged}">
                <f:selectItems value="#{language.countriesInMap}" />
            </h:selectOneMenu>

        </h:panelGrid>

    </h:form>

</h:body>
</html>

And my managed bean:

@ManagedBean(name="language")
@SessionScoped
public class LanguageBean implements Serializable{

    private static final long serialVersionUID = 1L;

    private String localeCode;

    private static Map<String,Object> countries;
    static{
        countries = new LinkedHashMap<String,Object>();
        countries.put("English", Locale.ENGLISH); //label, value
        countries.put("Chinese", Locale.SIMPLIFIED_CHINESE);
    }

    public Map<String, Object> getCountriesInMap() {
        return countries;
    }


    public String getLocaleCode() {
        return localeCode;
    }


    public void setLocaleCode(String localeCode) {
        this.localeCode = localeCode;
    }

    //value change event listener
    public void countryLocaleCodeChanged(ValueChangeEvent e){

        String newLocaleValue = e.getNewValue().toString();

        //loop country map to compare the locale code
        for (Map.Entry<String, Object> entry : countries.entrySet()) {

            if(entry.getValue().toString().equals(newLocaleValue)){

                FacesContext.getCurrentInstance()
                        .getViewRoot().setLocale((Locale)entry.getValue());

            }
        }
    }

}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Sviatlana
  • 1,728
  • 6
  • 27
  • 55
  • 2
    Your welcome file is wrong, remove it altogether (welcome file != home page file). You have mapped JSF on too many URL patterns, remove them all and keep only `*.xhtml`. You somehow have public views in /WEB-INF which is atypical for JSF. Put them outside. After all those changes retry. Not sure if that would work as you seem to also be using Spring, which I know nothing about (I just use Java EE, not Spring). Too much focused/confused on Spring MVC perhaps? This is food for thought: http://stackoverflow.com/q/18744910 – BalusC Feb 09 '16 at 08:18
  • Thank you! the problem was in welcome file location. I placed it into web app folder and everything is ok now – Sviatlana Feb 09 '16 at 09:28

0 Answers0