2013-12-17 192 views
0

我有一個Primefaces JSF項目,並與下面的註解我的豆子:會話範圍的bean不是一個會話範圍的bean

@Named("reportTabBean") 
@SessionScoped 
public class ReportTabBean implements Serializable { 
... 
} 

豆創建各種選項卡,樹木等的登錄使用四郎框架需要在用戶可以訪問應用程序之前。出於某種原因,每個瀏覽器會話共享相同的選項卡,樹等等以及它們的狀態。這就像bean是應用程序範圍。我嘗試了不同的示波器,沒有任何運氣。但是,用戶主體不共享。我收到了正確的登錄用戶。

我都試過

@javax.enterprise.context.SessionScoped

@javax.faces.bean.SessionScoped

沒有運氣。

我在做什麼錯?

編輯

我使用JDK 32年6月1日。

這裏的咖啡豆之一:

@Named("reportTabBean") 
@ViewScoped 
public class ReportTabBean implements Serializable { 
    private Map<String, TreeNode> model; //Accordian menu which I want initiated upfront 
    private int      activeReportTypeIndex; 
    private TreeNode    selectedNode; 
    .... 
    @PostConstruct 
    public void createModel() { 
     model = treeService.createModel(); 
    } 

    public Map<String, TreeNode> getModel() { 
     return model; 
    } 
    .... 
    public void tabIsChanged(TabChangeEvent event) { 
     FacesContext context = FacesContext.getCurrentInstance(); 
     Map<String, String> params = context.getExternalContext().getRequestParameterMap(); 
     AccordionPanel tabView = (AccordionPanel) event.getComponent(); 
     String activeIndexValue = params.get(tabView.getClientId(context) + "_tabindex"); 

     this.activeReportTypeIndex = Integer.parseInt(activeIndexValue); 
    } 

    public void onNodeSelect(final NodeSelectEvent event) { 
     FacesContext.getCurrentInstance().getExternalContext().getSession(true); 

     try { 
      tabs.add(new ReportListTab(
       event.getTreeNode().getData().toString(), 
       reportService.getReports(
         ((FolderTreeNodeImpl) event.getTreeNode()).getReportType(), 
         ((FolderTreeNodeImpl) event.getTreeNode()).getFolderID() 
         ) 
       ) 
       ); 
     } catch (HibernateException e) { 
      LOGGER.error("[onNodeSelect] HibernateException", e); 

      FacesContext.getCurrentInstance().addMessage(":messages", 
       new FacesMessage(
         FacesMessage.SEVERITY_ERROR, 
         "Fatal Error", 
         "Please try again. If the error occurs, please contact the administrator." 
       ) 
       ); 
     } 
} 


} 

@ViewScoped是org.omnifaces.cdi.ViewScoped但不要緊什麼範圍,我沒有。它表現爲應用程序範圍。如果我以用戶A的身份登錄Chrome並在一點處打開手風琴,則用戶B的Firefox手風琴中的同一點也將被打開。摺疊式菜單定義如下:

<h:form>        
     <p:accordionPanel value="#{reportTabBean.model.keySet().toArray()}" var="reportType" activeIndex="#{reportTabBean.activeReportTypeIndex}"> 

      <p:ajax event="tabChange" listener="#{reportTabBean.tabIsChanged}" /> 

      <p:tab title="${msg[reportType]}"> 

       <p:tree value="#{reportTabBean.model[reportType]}" var="node" dynamic="true" 
        cache="true" selectionMode="single" selection="#{reportTabBean.selectedNode}" id="tree"> 

        <p:ajax event="select" update=":tabViewForm" listener="#{reportTabBean.onNodeSelect}" /> 

        <p:treeNode type="node" expandedIcon="folder-open" collapsedIcon="folder-collapsed"> 
         <h:outputText value="#{node}" /> 
        </p:treeNode> 

        <p:treeNode type="leaf" icon="document-node"> 
         <h:outputText value="#{node}" /> 
        </p:treeNode> 

       </p:tree> 

      </p:tab> 
     </p:accordionPanel> 
    </h:form> 

下面是我的POM依賴關係:

<dependencies> 
     <!-- OmniFaces --> 
     <dependency> 
      <groupId>org.omnifaces</groupId> 
      <artifactId>omnifaces</artifactId> 
      <version>1.6.3</version> <!-- Or 1.7-SNAPSHOT --> 
     </dependency> 
     <dependency> 
      <groupId>com.sun</groupId> 
      <artifactId>tools</artifactId> 
      <version>1.6</version> 
     </dependency> 
     <dependency> 
      <groupId>javax</groupId> 
      <artifactId>javaee-web-api</artifactId> 
      <version>6.0</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax</groupId> 
      <artifactId>javaee-api</artifactId> 
      <version>6.0</version> 
      <scope>provided</scope> 
     </dependency> 

     <!-- R --> 
     <dependency> 
      <groupId>net.rforge.REngine</groupId> 
      <artifactId>REngine</artifactId> 
      <version>1.7</version> 
     </dependency> 
     <dependency> 
      <groupId>net.rforge.Rserve</groupId> 
      <artifactId>Rserve</artifactId> 
      <version>1.7</version> 
     </dependency> 

     <!-- Log4J --> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-log4j12</artifactId> 
      <version>1.6.1</version> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-api</artifactId> 
      <version>1.6.1</version> 
     </dependency> 
     <dependency> 
      <artifactId>log4j</artifactId> 
      <groupId>log4j</groupId> 
      <type>jar</type> 
      <version>1.2.16</version> 
     </dependency> 

     <dependency> 
      <groupId>cglib</groupId> 
      <artifactId>cglib</artifactId> 
      <version>2.2</version> 
     </dependency> 

     <!-- Spring --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-tx</artifactId> 
      <version>3.1.4.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>3.1.4.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
      <version>3.1.4.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-web</artifactId> 
      <version>3.1.4.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-config</artifactId> 
      <version>3.1.4.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-orm</artifactId> 
      <version>3.1.4.RELEASE</version> 
      <!-- will come with all needed Spring dependencies such as spring-core 
       and spring-beans --> 
     </dependency> 

     <!-- Hibernate --> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-entitymanager</artifactId> 
      <version>4.1.9.Final</version> 
      <!-- will come with Hibernate core --> 
     </dependency> 

     <dependency> 
      <groupId>org.glassfish.web</groupId> 
      <artifactId>el-impl</artifactId> 
      <version>2.2</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.tomcat</groupId> 
      <artifactId>el-api</artifactId> 
      <version>6.0.32</version> 
      <type>jar</type> 
      <scope>provided</scope> 
     </dependency> 

     <!-- PrimeFaces --> 
     <dependency> 
      <groupId>org.primefaces</groupId> 
      <artifactId>primefaces</artifactId> 
      <version>4.0</version> 
     </dependency> 
     <dependency> 
      <groupId>org.primefaces</groupId> 
      <artifactId>primefaces-mobile</artifactId> 
      <version>0.9.4</version> 
     </dependency> 

     <!-- JSF --> 
     <dependency> 
      <groupId>com.sun.faces</groupId> 
      <artifactId>jsf-api</artifactId> 
      <version>2.1.7</version> 
     </dependency> 
     <dependency> 
      <groupId>com.sun.faces</groupId> 
      <artifactId>jsf-impl</artifactId> 
      <version>2.1.7</version> 
     </dependency> 

     <dependency> 
      <groupId>javax.inject</groupId> 
      <artifactId>javax.inject</artifactId> 
      <version>1</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.2</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>javax.servlet-api</artifactId> 
      <version>3.0.1</version> 
     </dependency> 

     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <scope>test</scope> 
      <version>3.8.1</version> 
     </dependency> 

     <!-- Oracle --> 
     <dependency> 
      <groupId>com.oracle</groupId> 
      <artifactId>ojdbc6</artifactId> 
      <version>ojdbc6</version> 
     </dependency> 
     <dependency> 
      <groupId>com.oracle</groupId> 
      <artifactId>xdb</artifactId> 
      <version>11.2.0</version> 
     </dependency> 
     <dependency> 
      <groupId>com.oracle</groupId> 
      <artifactId>xmlparserv2</artifactId> 
      <version>11.2.0</version> 
     </dependency> 

     <!-- Apache Shiro authentication/authorization framework --> 
     <dependency> 
      <groupId>org.apache.shiro</groupId> 
      <artifactId>shiro-core</artifactId> 
      <version>1.1.0</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.shiro</groupId> 
      <artifactId>shiro-web</artifactId> 
      <version>1.1.0</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.shiro</groupId> 
      <artifactId>shiro-aspectj</artifactId> 
      <version>1.1.0</version> 
     </dependency> 
    </dependencies> 

回答

0

定了!原因是我使用Spring我需要使用Spring的範圍註釋而不是JSF。

@Scope("session") 

而不是

@SessionScoped 

我找到了解決辦法,從this thread

1

由於您使用@Named你應該使用@SessionScopedjavax.enterprise.context包,從CDI。

由於某種原因,每個瀏覽器會話共享相同的選項卡,樹等等以及它們的狀態。這就像豆類應用範圍

每一個瀏覽器選項卡訪問同一個Web應用程序將共享相同的用戶會話,所以@SessionScoped豆不表現爲@ApplicationScoped。如果您在不同的瀏覽器中打開相同的Web應用程序,可以注意到這一點Chrome和Firefox,你會看到你正在使用兩個完整的不同會話。

通過描述您的問題,您需要使用@ViewScoped。如果您正在使用Java EE 7,則可以使用JSF 2.2附帶的此範圍:@javax.faces.view.ViewScoped。如果您正在使用Java EE 6或之前的版本,最簡單的解決方案將使用@ViewScoped from OmniFaces(請注意,您至少需要OmniFaces 1.6)或使用@ViewScoped from MyFaces CODI

相關Q /作爲:

+0

我嘗試了2種不同的瀏覽器,並以2個不同的用戶身份登錄到Web應用程序,但仍然看到了問題。甚至在不同的電腦上也是如此。我會嘗試@ViewScoped選項。我正在使用Java EE 6. OmniFaces會與PrimeFaces發生衝突嗎? – raylee

+0

@raylee OmniFaces不會與PrimeFaces發生衝突,因爲OmniFaces是一個JSF實用程序庫。實際上,OmniFaces展示櫃是使用JSF和PrimeFaces(以及OmniFaces)編寫的 –

+0

我從OmniFaces實現了@ViewScoped,但共享狀態的問題仍然存在。 – raylee

相關問題