2013-07-27 40 views
0

我有一个需求,当用户从下拉列表中选择一种语言时,我需要以不同语言显示相同的页面。 为此,我使用多种语言的selectOneMenu。当用户选择语言(区域设置)时,该值应该被附加到URL。在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(); 
} 

任何帮助,将不胜感激。

回答

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(); 
} 
0

这听起来像一个很好的候选人在@SessionScoped豆存储。在bean中设置语言,它可以被用户请求的所有页面使用(当然在本次会话过程中)。

如果用户想要链接到特定语言的URL,则可以选择拦截请求并从url中设置语言并进行适当重定向的自定义过滤器。不管你是否真的想要让自己满意......