Session Scoped Bean不是Session Scoped Bean

时间:2013-12-17 03:38:30

标签: java spring jsf session primefaces

我有一个Primefaces JSF项目,并使用以下内容注释我的bean:

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

bean创建各种选项卡,树等。在用户可以访问应用程序之前,需要使用Shiro框架进行登录。由于某种原因,每个浏览器会话共享相同的选项卡,树等以及它们的状态。就像bean是应用程序范围一样。我尝试了不同的范围而没有任何运气。但是,用户主体不是共享的。我正在获取正确的登录用户。

我试过了两次

@javax.enterprise.context.SessionScoped

@javax.faces.bean.SessionScoped

没有运气。

我做错了什么?

修改

我正在使用JDK 1.6.32。

这是其中一个豆子:

@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>

2 个答案:

答案 0 :(得分:2)

由于您使用的是@Named,因此您应该使用来自CDI的@SessionScoped包中的javax.enterprise.context

  

由于某种原因,每个浏览器会话共享相同的选项卡,树等以及它们的状态。这就像bean是应用程序范围

访问同一个Web应用程序的每个浏览器选项卡都将共享同一个用户会话,因此@SessionScoped bean的行为不是@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

相关问答:

答案 1 :(得分:0)

修正了它!事实证明这是因为我使用Spring我需要使用Spring的范围注释而不是JSF。

@Scope("session")

而不是

@SessionScoped

我从this thread找到了解决方案。