JSF语言环境更改侦听器不是持久性的

JSF语言环境更改侦听器不是持久性的

本文介绍了JSF语言环境更改侦听器不是持久性的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WebApp中,我创建了一个托管Bean,使我可以使用事件更改侦听器将语言环境从法语更改为英语,反之亦然.

In my WebApp I created a managed Bean that allows me to change the Locale from French to english and vice versa using Event Change Listener.

      package beans;

    import java.util.Locale;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class LocaleBean {

    private Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();

    public Locale getLocale() {
        return locale;
    }

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

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

}

并在我的template.xhtml中:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="#{localeBean.language}"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.prime.com.tr/ui"
      xmlns:f="http://java.sun.com/jsf/core">
    <f:view contentType="text/html" locale="#{localeBean.locale}"  id="mescoca">
        <h:head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <title><ui:insert name="title"></ui:insert></title>
            <h:outputStylesheet name="css/jsfcrud.css"/>
            <!--<f:loadBundle var="bundle" basename="/Bundle"/> -->
        </h:head>

        <h:body style="font-size: small; font-family: Ubuntu,verdana;">
          <h:form>
     <p:panel closable="false" style="float: right;height: 50px;font-size: smaller" >
     <h:panelGrid columns="2" style="text-align: center">
       <h:outputText value="#{bundle.Language}"/>
       <h:selectOneMenu value="#{localeBean.language}"                                 onchange="submit()">
     <f:selectItem itemValue="fr" itemLabel="Français" />
      <f:selectItem itemValue="en" itemLabel="English" />
       <f:selectItem itemValue="fr_FR" itemLabel="France"/>
        <f:selectItem itemValue="en_US" itemLabel="US" />
     </h:selectOneMenu>
        </h:panelGrid>
      </p:panel>

其他页面:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">

    <ui:composition template="/template.xhtml">
        <ui:define name="title">
            <h:outputText value="#{bundle.EditHistoryTitle}"></h:outputText>
        </ui:define>
        <ui:define name="body" >
            <h:panelGroup id="messagePanel" layout="block">
                <h:messages errorStyle="color: red" infoStyle="color: green" layout="table"/>
            </h:panelGroup>
            <h:form>.....

它正在工作,但是问题在于,一旦我导航到另一个页面,该语言就会恢复到其第一个值.否则,当我更改语言时,它只会影响当前页面,而当我移至另一页面时,本地化包将在 faces-config.xml

It is working, but the problem is that the language gets back to its first value once I navigate to another page. Otherwise, When I change the language, it only affects the current page and once I move to another page, the localization bundle gets its default value in faces-config.xml

我需要的是使语言在整个会话中保持不变.有人知道线索吗?

What I need is to make the language persistent through the whole session.Does anyone have a clue plz?

推荐答案

您需要将主模板包装在

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

另请参见:

08-11 05:09