FacesContext#getViewRoot()在设置< f:view语言环境>时返回null.第一次 [英] FacesContext#getViewRoot() returns null while setting <f:view locale> for first time

查看:162
本文介绍了FacesContext#getViewRoot()在设置< f:view语言环境>时返回null.第一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个页面中更改我的JSF应用程序语言环境,而这必须更改我所有页面的语言环境.我已遵循此链接,并且在JSF,如何记住每个会话而不是每个请求/视图的选定语言环境

如果我运行该应用程序,则可以在我的 index.xhtml 中更改语言环境,并且该语言环境是按会话设置的,因此,如果要访问第 index_1.xhtml 页我会看到语言环境已更改.

我的问题是,当我运行应用程序并编写URL时: http: //localhost:8080/Myapp-war/faces/index_.xhtml 转到 index_1.xhtml 页,我更改了index.xhtml中语言环境未更改的语言环境. /p>

这是我的代码:

Managed Bean

package controllers;

import java.io.Serializable;
import java.util.Locale;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

    @ManagedBean
    @SessionScoped
    public class LanguageSwitcher implements Serializable{

        private Locale locale; 


        @PostConstruct
        public void init() 
        {
            locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
        }


       public Locale getLocale() {
            return locale;
        }

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

        //Change locale
        public void changeLanguage(String language) {
            locale = new Locale(language);
            FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
        }   

    }

文件index.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html   lang="#{languageSwitcher.language}"
        xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">


    <h:head>
    </h:head>

    <h:body>     
        <h3>Language switcher:</h3>
        <h:form id="language_form">
            <h:commandLink action="#{languageSwitcher.changeLanguage('es')}" value="Español" rendered="#{languageSwitcher.language != 'es'}"/> |
            <h:commandLink action="#{languageSwitcher.changeLanguage('en')}" value="English" rendered="#{languageSwitcher.language != 'en'}"/>
        </h:form>

       <h:outputText value="#{msg['greeting']}" />

    </h:body>
</html>

文件index_1.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html   lang="#{languageSwitcher.language}"
        xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

    <f:view locale="#{languageSwitcher.locale}">

    <h:head>
    </h:head>

    <h:body>     
        <h3>Language switcher:</h3>
        <h:form id="language_form">
            <h:commandLink action="#{languageSwitcher.changeLanguage('es')}" value="Español" rendered="#{languageSwitcher.language != 'es'}"/> |
            <h:commandLink action="#{languageSwitcher.changeLanguage('en')}" value="English" rendered="#{languageSwitcher.language != 'en'}"/>
        </h:form>

       <h:outputText value="#{msg['greeting']}" />

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

如果我在index.xhtml中写入<f:view locale="#{languageSwitcher.locale}">,则在运行应用程序时会出现错误.

我该如何解决?

更新:

运行该应用程序时出现的错误是:

java.lang.NullPointerException 在controllers.LanguageSwitcher.init(LanguageSwitcher.java:56)

解决方案

我转载了您的问题.这是 issue 3021 的结果,该问题自Mojarra 2.2.5开始应用.现在,在视图构建期间确定语言环境.以前,在编写找到的答案时,语言环境是在视图渲染期间确定的,这允许代码以这种方式找到视图的默认语言环境.但是,在视图构建期间这是不可能的,因为该视图尚不存在.您会看到,getViewRoot()返回了null.

您需要从外部上下文获取请求语言环境.

@PostConstruct
public void init() {
    locale = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
}

您找到的答案也已更改.

I am trying to change my JSF application locale in one page and that has to change all my pages locale. I have followed this link, and it works well Localization in JSF, how to remember selected locale per session instead of per request/view

If I run the application I can change the locale in my index.xhtml and that locale is set per session, so if then I go to page index_1.xhtml I will see the locale changed.

My problem is that when I run the application and I write the URL: http://localhost:8080/Myapp-war/faces/index_.xhtml to go to the index_1.xhtml page and I change the locale that locale is not changed in index.xhtml.

This is my code:

Managed Bean

package controllers;

import java.io.Serializable;
import java.util.Locale;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

    @ManagedBean
    @SessionScoped
    public class LanguageSwitcher implements Serializable{

        private Locale locale; 


        @PostConstruct
        public void init() 
        {
            locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
        }


       public Locale getLocale() {
            return locale;
        }

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

        //Change locale
        public void changeLanguage(String language) {
            locale = new Locale(language);
            FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
        }   

    }

File index.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html   lang="#{languageSwitcher.language}"
        xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">


    <h:head>
    </h:head>

    <h:body>     
        <h3>Language switcher:</h3>
        <h:form id="language_form">
            <h:commandLink action="#{languageSwitcher.changeLanguage('es')}" value="Español" rendered="#{languageSwitcher.language != 'es'}"/> |
            <h:commandLink action="#{languageSwitcher.changeLanguage('en')}" value="English" rendered="#{languageSwitcher.language != 'en'}"/>
        </h:form>

       <h:outputText value="#{msg['greeting']}" />

    </h:body>
</html>

File index_1.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html   lang="#{languageSwitcher.language}"
        xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

    <f:view locale="#{languageSwitcher.locale}">

    <h:head>
    </h:head>

    <h:body>     
        <h3>Language switcher:</h3>
        <h:form id="language_form">
            <h:commandLink action="#{languageSwitcher.changeLanguage('es')}" value="Español" rendered="#{languageSwitcher.language != 'es'}"/> |
            <h:commandLink action="#{languageSwitcher.changeLanguage('en')}" value="English" rendered="#{languageSwitcher.language != 'en'}"/>
        </h:form>

       <h:outputText value="#{msg['greeting']}" />

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

If I write <f:view locale="#{languageSwitcher.locale}">in my index.xhtml, when I run the app I get an error.

How could I solve it?

UPDATE:

The error I get when running the app is:

java.lang.NullPointerException at controllers.LanguageSwitcher.init(LanguageSwitcher.java:56)

解决方案

I reproduced your problem. This is the consequence of issue 3021 which was applied since Mojarra 2.2.5. The locale is now determined during view build time. Previously, at time of writing the answer you found, the locale was determined during view render time which allowed the code to find view's default locale this way. However, during view build time this is not possible as the view doesn't exist yet. You see, the getViewRoot() returned null.

You need to obtain the request locale from the external context instead.

@PostConstruct
public void init() {
    locale = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
}

The answer you found has also been altered.

这篇关于FacesContext#getViewRoot()在设置&lt; f:view语言环境&gt;时返回null.第一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆