此代码已完全简化,但重现了我的问题:
BackingBean.java
public String reload(){
System.out.println(FacesContext.getCurrentInstance()
.getViewRoot().getLocale());
return "test";
}
public void setLocale(){
System.out.println("locale changed!");
FacesContext.getCurrentInstance()
.getViewRoot().setLocale(Locale.FRANCE);
}
测试.xhtml
<h:form>
<h:commandLink action="#{backingBean.reload}" value="reload page"/>
</h:form>
<h:form>
<h:commandLink action="#{backingBean.setLocale}" value="change locale"/>
</h:form>
输出:
en
locale changed!
fr_FR
en
如果您将更改语言环境,然后调用reload
两次方法,则语言环境将重置为 default en
。locale
重置的原因是什么?此外,仅在转发到其他页面的情况下才会发生,如果您将reload
方法更改为void
,则区域设置将保持不变fr
。
public void reload(){
System.out.println(FacesContext.getCurrentInstance()
.getViewRoot().getLocale());
}
输出:
en
locale changed!
fr_FR
fr_FR
但经过 2 次转发后,语言环境将改回en