public class LocaleViewHandler extends ViewHandler{
private final ViewHandler base;
public LocaleViewHandler(ViewHandler base){
super();
this.base = base;
}
@Override
public Locale calculateLocale(FacesContext context) {
Locale locale;
HttpSession session = (HttpSession) context.getExternalContext()
.getSession(false);
if (session != null) {
//Return the locale saved by the managed bean earlier
locale = (Locale) session.getAttribute("locale");
if (locale == null) {
locale= new Locale("en");
}
return locale;
}
return base.calculateLocale(context);
}
@Override
public String calculateRenderKitId(FacesContext arg0) {
// TODO Auto-generated method stub
return base.calculateRenderKitId(arg0);
}
@Override
public UIViewRoot createView(FacesContext arg0, String arg1) {
// TODO Auto-generated method stub
return base.createView(arg0, arg1);
}
@Override
public String getActionURL(FacesContext arg0, String arg1) {
// TODO Auto-generated method stub
return base.getActionURL(arg0, arg1);
}
@Override
public String getResourceURL(FacesContext arg0, String arg1) {
// TODO Auto-generated method stub
return base.getResourceURL(arg0, arg1);
}
@Override
public void renderView(FacesContext arg0, UIViewRoot arg1)
throws IOException, FacesException {
// TODO Auto-generated method stub
base.renderView(arg0, arg1);
}
@Override
public UIViewRoot restoreView(FacesContext arg0, String arg1) {
// TODO Auto-generated method stub
return base.restoreView(arg0, arg1);
}
@Override
public void writeState(FacesContext arg0) throws IOException {
// TODO Auto-generated method stub
base.writeState(arg0);
}
public ViewHandler getBase() {
return base;
}
}
我正在尝试内化示例,我编写了如上所示的视图处理程序。我有两个地方英语和法语。但写完这个处理程序之后。控制台上没有错误,但它显示空白页面。可以看到视图..
答案 0 :(得分:3)
您应该扩展ViewHandlerWrapper
而不是自己编写ViewHandler
。
public class LocaleViewHandler extends ViewHandlerWrapper {
private ViewHandler wrapped;
public LocaleViewHandler(ViewHandler wrapped) {
this.wrapped = wrapped;
}
@Override
public ViewHandler getWrapped() {
return wrapped;
}
@Override
public Locale calculateLocale(FacesContext context) {
// Do your thing here.
}
}
无关,这不是完全正确的方法。你应该使用
<f:view locale="#{localeBean.locale}">
代替。另请参阅Localization in JSF, how to remember selected locale per session instead of per request/view。