JSF 区域设置更改侦听器不持久

发布于 2024-11-09 13:01:22 字数 3535 浏览 3 评论 0原文

在我的 WebApp 中,我创建了一个托管 Bean,它允许我使用事件更改侦听器将区域设置从法语更改为英语,反之亦然。

      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 中获取其默认值,

我需要的是使语言在整个会议期间坚持不懈。 请问有人有线索吗?

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);
    }

}

and in my 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>

The others pages:

<?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>.....

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

走过海棠暮 2024-11-16 13:01:22

您需要将主模板包装在

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

另请参阅:

You need to wrap your master template in a

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

See also:

筱武穆 2024-11-16 13:01:22

问题是,当您在 valueChangeListener 中设置 Locale 时,您是将其设置在 ViewRoot 上,该 ViewRoot 随当前视图而不是会话一起生存和消亡。

您将 LocaleCode 存储在 SessionScoped 托管 bean 中,以便您可以在 ViewRoot 中为每个页面设置 Locale,这应该可以解决您的问题。

The problem is that when you are setting your Locale in the valueChangeListener you are setting it on the ViewRoot which lives and dies with the current view, and not the session.

You are storing your LocaleCode in the SessionScoped managed bean so you can set the Locale in the ViewRoot for every page and this should fix your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文