我有一个链接,可以在xhtml中从lang 1切换到lang 2:
<p:commandLink value="#{msgs['home.french']}" id="frenchLink"
rendered="#{mainBean.changeLanguage.getLanguage() ne 'fr'}"
actionListener="#{mainBean.refreshPage}" ajax="false">
<f:param name="selectedLanguage" value="fr"/>
</p:commandLink>
<p:commandLink value="#{msgs['home.english']}" id="englishLink" r
endered="#{mainBean.changeLanguage.getLanguage() ne 'en'}" actionListener="#{mainBean.refreshPage}" ajax="false">
<f:param name="selectedLanguage" value="en"/>
</p:commandLink>
并在mainBean中,
public void refreshPage(ActionEvent event){
FacesContext context = FacesContext.getCurrentInstance();
String language = context.getExternalContext().getRequestParameterMap().get("selectedLanguage");
changeLanguage = new ChangeLanguage();
ChangeLanguage.setLanguage(language);
String viewId = context.getViewRoot().getViewId();
ViewHandler handler = context.getApplication().getViewHandler();
UIViewRoot root = handler.createView(context, viewId);
root.setViewId(viewId);
context.setViewRoot(root);
context.renderResponse();
}
始终获取未捕获的TypeError:对象#在primefaces.js上没有方法'replace'
只有当我切换到法语和具有selectOneMenu,对话框的页面时,才会弹出这些问题。
对此有任何指示赞赏。
App使用Jsf 2.0,Primefaces 3.5
答案 0 :(得分:-1)
在yor faces-config.xml中定义默认语言
<application>
<locale-config>
<default-locale>en</default-locale>
</locale-config>
</application>
建议您的bean为@SessionScoped
,然后考虑创建一个bean来控制系统的位置。
在Bean中创建此属性。
private static Map<String,Locale> countries;
static{
countries = new HashMap<String,Locale>();
countries.put("fr", Locale.FRANCE);
countries.put("en", Locale.ENGLISH);
}
添加更改语言的方法。
public void changeLanguage(){
FacesContext context = FacesContext.getCurrentInstance();
String language = context.getExternalContext().getRequestParameterMap().get("selectedLanguage");
context.getViewRoot().setLocale(countries.get(language));
}
在您的视图中(xhtml):
<p:commandLink value="#{msgs['home.french']}" id="frenchLink"
rendered="#{mainBean.changeLanguage.getLanguage() ne 'fr'}"
actionListener="#{mainBean.changeLanguage}" ajax="false"
oncomplete="javascript:location.reload(true)">
<f:param name="selectedLanguage" value="fr"/>
</p:commandLink>
<p:commandLink value="#{msgs['home.english']}" id="englishLink"
rendered="#{mainBean.changeLanguage.getLanguage() ne 'en'}" actionListener="#{mainBean.changeLanguage}" ajax="false"
oncomplete="javascript:location.reload(true)">
<f:param name="selectedLanguage" value="en"/>
</p:commandLink>