我有一个简单的表单,它应该在我的 Login-Bean 中调用 countryChanged() 方法。
<h:form id="languageForm">
<p:selectOneMenu value="#{login.locale}" var="locale" effect="fold">
<p:ajax listener="#{login.countryChanged}" />
<f:selectItem itemLabel="#{gui.LANGUAGE}" itemValue=""></f:selectItem>
<f:selectItems value="#{login.locales}" var="locales" itemLabel="#{locales.displayLanguage}" itemValue="#{locales}" />
<p:column><h:graphicImage library="images" name="flags/#{locale.country}.png" /></p:column>
<p:column>#{locale.displayName}</p:column>
</p:selectOneMenu>
</h:form>
<h:form id="loginForm">
<h:panelGrid columns="2" title="Flexicon Login">
<h:outputLabel for="login">#{gui.LOGIN}</h:outputLabel>
<h:inputText id="login" required="true" value="#{loginControl.login}" label=""></h:inputText>
<h:outputLabel for="password">#{gui.PASSWORD}</h:outputLabel>
<h:inputSecret id="password" value="#{loginControl.password}"></h:inputSecret>
</h:panelGrid>
<p:messages></p:messages>
<h:commandButton value="#{gui.BTN_LOGIN}" action="#{loginControl.login}"></h:commandButton>
</h:form>
登录豆
@ManagedBean(name="login")
@SessionScoped
public class LoginControl implements Serializable {
private static final long serialVersionUID = 5887191233181008116L;
private String login;
private String password;
private Locale locale;
private List<Locale> locales;
public LoginControl(){
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Locale getLocale() {
if(locale==null){
locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
}
return locale;
}
public void setLocale(Locale locale) {
//FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
this.locale = locale;
}
public List<Locale> getLocales() {
if(locales==null){
Logger.getLogger(LoginControl.class).info("initializing locales...");
locales = new ArrayList<Locale>();
locales.add(Locale.GERMANY);
locales.add(Locale.UK);
locales.add(Locale.FRANCE);
}
return locales;
}
public void setLocales(List<Locale> locales) {
this.locales = locales;
}
public void countryChanged(){
FacesContext.getCurrentInstance().getViewRoot().setLocale(getLocale());
}
}
我遵循了 stackoverflow 中已经讨论过的几个建议。但不知何故,它不想工作。有人能看出错误吗?