在URL中传递区域设置值

时间:2013-07-27 13:06:03

标签: jsf primefaces localization

我有一个要求,当用户从下拉列表中选择语言时,我需要以不同的语言显示相同的页面。 为此,我使用了具有多种语言的selectOneMenu。当用户选择语言(语言环境)时,该值应附加到URL。

我使用了以下代码,但是它使用locale替换了url中已存在的参数。 有没有办法可以附加locale参数而不会干扰已经存在的参数。

FacesContext ctx = FacesContext.getCurrentInstance();
String contxRoot = ctx.getExternalContext().getRequestContextPath();
String viewId = ctx.getViewRoot().getViewId();
String URL=viewId+"?language="+this.selectedLaguage;

try {
    FacesContext.getCurrentInstance().getExternalContext().redirect(contxRoot+URL);
} catch (IOException e) {
    e.printStackTrace();
}

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

此代码不是美女,但您可以尝试

FacesContext ctx = FacesContext.getCurrentInstance();
String contxRoot = ctx.getExternalContext().getRequestContextPath();
String initialUri = ((HttpServletRequest) ctx.getExternalContext().getRequest()).getRequestURI();
String languageParameter = "?language="+this.selectedLaguage;

try {
FacesContext.getCurrentInstance().getExternalContext().redirect(contxRoot+initialUri+languageParameter);
} catch (IOException e) {
e.printStackTrace();
}

答案 1 :(得分:0)

这听起来像是存储在@SessionScoped bean中的好选择。在bean中设置语言,它可以被用户请求的所有页面使用(当然,在本课程的过程中)。

例如,如果用户想要链接到特定语言的网址,则拦截请求并从网址设置语言并进行适当重定向的自定义过滤器可以是选项。你是否真的想要允许这取决于你......

相关问题