SessionScoped bean创建了好几次

时间:2016-11-07 22:05:26

标签: tomcat java-ee primefaces jsf-2.2

我使用Eclipse Mars.2,该系统具有64位的Java 1.8更新102,但该项目使用Java 1.7,JSF Mojarra 2.2,Primefaces 6.0和Tomcat 7.0.53 / 7.0.54 / 8.0.1 / 8.0.11 。 我创建了最小的测试项目以显示问题。

测试项目包含2个使用模板创建的页面,顶部有菜单栏可供浏览,顶部有一个selectOneMenu可选择当前语言。 当前语言存储在会话bean中的变量中,该会话bean在两个页面中都使用,并且每次更改selectOneMenu时都会刷新。 当我在Tomcat 7.0.53 / Tomcat 8.0.1或更低版本上运行APP时,它完美地工作,SessionScoped bean只创建一次,并且值保持正确存储。 当我在Tomcat 7.0.54 / Tomcat 8.0.11或更高版本中运行相同的APP时会出现问题,在这种情况下,每次使用时都会创建SessionScoped bean,并且当初语言值初始化时,它始终设置为默认(英文)。

我已经研究了很多并在几个网站上询问但没有任何运气。 也许有人可以照亮我? 非常感谢提前!!

当我使用模板时,页面总是由head_view.xhtml + index_view.xhtml / page2_view.xhtml + foot_view.xhtml(index_view.xhtml = page2_view.xhtml,脚部分为空)组成

head_view.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:p="http://primefaces.org/ui">

  <f:view locale="#{sesion.currentLanguage}">   
    <h:head></h:head>
    <h:body>
        <h:form>
            <p:selectOneMenu id="selectlanguage" value="#{sesion.selectedLanguage}" converter="languageConverter" var="t" >
                  <f:selectItems value="#{sesion.languages}" var="idi" itemLabel="#{idi.name}" itemValue="#{idi}" />

                  <p:column>
                      <h:outputText value="#{t.name}" />
                  </p:column>  

                  <p:ajax event="change" listener="#{sesion.changeLanguage}" />
             </p:selectOneMenu>

             <p:menubar>

                 <p:menuitem value="#{msg.btn1}" action="#{headBB.goTo('index')}" />

                 <p:menuitem value="#{msg.btn2}" action="#{headBB.goTo('page2')}" />

             </p:menubar>                                                                                               
        </h:form>
    </h:body>
  </f:view>
</html>

index_view.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:p="http://primefaces.org/ui">

  <f:view locale="#{sesion.currentLanguage}">
    <h:head></h:head>
    <h:body>
        <h:form> 
        INDEX<br/>           
        #{msg.random_text}
        </h:form>
    </h:body>
  </f:view> 
</html>

headBB.java

package beans;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@RequestScoped
public class HeadBB implements Serializable {


    private static final long serialVersionUID = 7510243725613503561L;

    public HeadBB() {

    }

    public String goTo(String pageName){

        String url;

        switch (pageName) {
        case "index":
            getsesion().setCurrentPage("index");
            url = "index?faces-redirect=true";
            break;
        case "page2":
            getsesion().setCurrentPage("rent");
            url = "page2?faces-redirect=true";
            break;
        default:
            getsesion().setCurrentPage("index");
            url = "index?faces-redirect=true";
            break;
        }

        return url;
    }

    public static Sesion getsesion() { 
        FacesContext context = FacesContext.getCurrentInstance(); 
        return (Sesion) context.getApplication().evaluateExpressionGet(context, "#{sesion}", Sesion.class);       
    }    

}

Language.java

package beans;

import java.io.Serializable;

public class Language implements Serializable {

    private static final long serialVersionUID = 8302229459482632486L;

    private String code;
    private String name;

    public Language(String code, String name) {
        this.code = code;
        this.name = name;
    }
    //getter and setters
}

sesion.java

package beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.annotation.PostConstruct;
import javax.faces.application.NavigationHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;


@ManagedBean
@SessionScoped
public class Sesion implements Serializable {

    private static final long serialVersionUID = 5845708623625499704L;

    private String currentPage = "";
    private Locale currentLanguage;
    private Language selectedLanguage;
    private List<Language> languages;

    public Sesion() {

    }

    @PostConstruct
    public void init() {
        currentPage = "index";
        currentLanguage = new Locale("en", "GB");
        FacesContext.getCurrentInstance().getViewRoot().setLocale(currentLanguage);

        languages = new ArrayList<Language>();
        languages.add(new Language("ES", "Espanol"));
        languages.add(new Language("EN", "English"));

        selectedLanguage = new Language("EN", "English");
    }

    public void changeLanguage() {
        Locale tempLocale = new Locale("en", "GB");
        switch (selectedLanguage.getCode()) {
        case "ES":
            tempLocale = new Locale("es", "ES");
            break;
        default:
            tempLocale = new Locale("en", "GB");
            break;
        }
        FacesContext.getCurrentInstance().getViewRoot().setLocale(tempLocale);
        this.setCurrentLanguage(tempLocale);

        // we refresh ALL browsing to the current page
        FacesContext context = FacesContext.getCurrentInstance();
        NavigationHandler navigationHandler = context.getApplication().getNavigationHandler();
        navigationHandler.handleNavigation(context, null, this.currentPage + "?faces-redirect=true");
    }
    //getter and setters    
}

非常感谢你的时间!

0 个答案:

没有答案