我试图徒劳地使用primefaces更改语言环境,这是我的bean代码:
// more imports here
@ManagedBean
@SessionScoped
public class DateBean implements Serializable{
private Date startDate, endDate;
private Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
public void setLocale(Locale locale) {
this.locale = locale;
}
public Locale getLocale(){
return locale;
}
public void changeLocale(String loc){
FacesContext context = FacesContext.getCurrentInstance();
locale = new Locale(loc);
context.getViewRoot().setLocale(locale);
}
}
小脸:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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:p="http://primefaces.org/ui"
xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core">
<f:view locale="#{dateBean.locale}">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
<h:outputScript library="scripts" name="#{dateBean.locale}.js"></h:outputScript>
</h:head>
<h:body>
<h:form>
<p:commandButton value="de" action="#{dateBean.changeLocale('de')}" >
<p:ajax update="@form" process="@form"></p:ajax>
</p:commandButton>
<p:calendar id="cc" value="#{dateBean.startDate}" required="true" showOn="both"
requiredMessage="Start date required"/>
<p:message for="cc"></p:message>
</h:form>
</h:body>
</f:view>
</html>
当我单击语言环境按钮时,日历语言环境不会更改为德语,我也没有例外。然而,以这种方式使用旧的 JSF2.* CommandButton 组件完成这项任务就像轻而易举:
<h:commandButton value="portugal" action="#{dateBean.changeLocale('pt')}" >
<f:ajax render="@form"></f:ajax>
</h:commandButton>
请大家帮我弄清楚好吗?