i18n通过h:link或h:按钮

时间:2012-01-05 23:25:30

标签: java jsf

所有教程都通过 h:selectOneMenu 或其他类型的选择来实现i18n。有没有办法通过h:按钮实现i18n至少?我尝试将bean方法分配给结果属性,但它会引发错误。分配bean方法 action 属性根本没有效果。

<h:button outcome="#{languageController.currentLanguage}" value = "#{appMessage['link.label.language.english']}">
   <f:param name="selectedLanguage" value="en" />
</h:button>

public String changeLanguage(){
    FacesContext facesContext = FacesContext.getCurrentInstance();
    String selectedLocale = facesContext.getExternalContext().getRequestParameterMap().get("selectedLanguage");
    this.currentLanguage = selectedLocale;
    facesContext.getViewRoot().setLocale(new Locale(currentLanguage));
    return "welcome";
}

1 个答案:

答案 0 :(得分:0)

<h:link><h:button>发送GET请求,而不是POST请求。 outcome值需要指向目标视图。您需要设置视图参数,并通过<f:viewParam><f:event type="preRenderView">在目标视图中执行操作。

<h:button outcome="welcome" value="#{appMessage['link.label.language.english']}">
    <f:param name="selectedLanguage" value="en" />
</h:button>

with welcome.xhtml(省略验证)

<f:metadata>
    <f:viewParam name="selectedLanguage" value="#{languageController.currentLanguage}" />
    <f:event type="preRenderView" listener="#{languageController.changeLanguage}" />
</f:metadata>

并在bean中

public void changeLanguage() {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    facesContext.getViewRoot().setLocale(new Locale(currentLanguage));
}

另见: