调用f:从ViewScope查看区域设置

时间:2017-02-28 19:48:15

标签: jsf primefaces tomcat7

将Tomcat7与CDI WELD 2.4.1结合使用。 JSF 2.2。

我有一个viewcope DasshBoardBean javax.faces.view.ViewScoped,在dashboard.xhtml中,我使用像<f:view locale="#{dashBoardBean.locale}">这样的bean来设置或获取之前从loginbean设置的语言环境。

e.g。一个用户调用login-page并希望并更改通过url参数这样的login.xhmt?locale=fr logiBean也是javax.faces.view.ViewScoped

loginBean将默认语言从德语(de)更改为法语(fr)。使用新语言显示登录页面,但是当我重定向到仪表板时,会返回默认语言德语。

在login.xhtml中 <f:viewParam name="locale" value="#{loginBean.sessionBean.locale}" converter="#{loginBean.sessionBean.localeConverter}"/>

本地是在SessionBean中设置的,但在仪表板中,jsf-tag <f:view locale="#{dashBoardBean.locale}">不能正常工作,我有以下错误

WARNUNG: Using de_DE for locale because expression /shop/dashboard.xhtml @54,43 locale="#{dashBoardBean.locale}" returned null

如何在dashboard.xhtml中获取用户语言?

感谢您的帮助。

public class BaseBean {  
            @Inject  
            SessionBean sessionBean;  
            public SessionBean getSessionBean() {  
                return this.sessionBean;  
            }  
            public Locale getLocale() {  
                return getSessionBean().getLocale();  
            }  
        }  

        @Named  
        @ViewScoped  
        public class DashboardBean extends BaseBean implements Serializable {  
        }  

    // SessionBean
    import javax.enterprise.context.SessionScoped;
    import javax.inject.Named;

    @Named
    @SessionScoped
    public class SessionBean implements Serializable {
    private Locale locale;

    public Locale getLocale() {
            if (this.locale == null) {
                this.locale = Locale.getDefault();
            }

            FacesContext.getCurrentInstance().getViewRoot().setLocale(this.locale);
            return this.locale;
        }
    }

// set locale when user set the url param e.g. locale=en
    public Locale getLocale(String locale) {
            String locale2 = null;
            if (locale.indexOf("_") > 0) {
                locale2 = locale.split("_")[0];
            } else {
                locale2 = locale + "_" + locale.toUpperCase();
            }

            for (Locale l : Arrays.asList(Locale.getAvailableLocales())) {
                if (l.toString().toLowerCase().equals(locale) || l.toString().toLowerCase().equals(locale2)) {
                    this.locale = l;
                    break;
                }
            }

            if (this.locale == null) {
                this.locale = Locale.getDefault();
            }

            FacesContext.getCurrentInstance().getViewRoot().setLocale(this.locale);
            return this.locale;
        }

        public void setLocale(Locale locale) {
            this.locale = locale;
        }

        public Converter getLocalConverter() {
            return new Converter() {

                @Override
                public String getAsString(FacesContext context, UIComponent component, Object value) {
                    // TODO Auto-generated method stub
                    return (value == null) ? null : ((Locale) value).getLanguage();
                }

                @Override
                public Object getAsObject(FacesContext context, UIComponent component, String value) {
                    // TODO Auto-generated method stub
                    return (value == null) ? null : getLocale(value);
                }
            };
        }

DasboardBean从BaseBean调用getLocale。 BaseBean没有@Named或@ViewScoped Annotation

0 个答案:

没有答案