gpt4 book ai didi

java - session 范围 Bean 不是 session 范围 Bean

转载 作者:行者123 更新时间:2023-12-02 06:24:57 27 4
gpt4 key购买 nike

我有一个 Primefaces JSF 项目,并且我的 Bean 注释有以下内容:

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

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

我都尝试过

@javax.enterprise.context.SessionScoped

@javax.faces.bean.SessionScoped

运气不好。

我做错了什么?

编辑

我使用的是 JDK 1.6.32。

这是其中一颗 bean :

@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 中打开 Accordion 中的某一点,则用户 B 的 Firefox 中的 Accordion 中的同一点也会打开。 Accordion 菜单定义如下:

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

最佳答案

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

For some reason every browser session shares the same tabs, tree, etc. and the state of them. It's like the beans are application scope

访问同一 Web 应用程序的每个浏览器选项卡都将共享相同的用户 session ,因此 @SessionScoped bean 的行为与 @ApplicationScoped 不同。如果您在不同的浏览器中打开相同的 Web 应用程序(例如, Chrome 和 Firefox,您将看到您正在使用两个完全不同的 session 。

根据您的问题描述,您需要使用@ViewScoped。如果您使用的是 Java EE 7,则可以使用 JSF 2.2 附带的范围:@javax.faces.view.ViewScoped 。如果您使用的是 Java EE 6 或之前的版本,最简单的解决方案是使用 @ViewScoped from OmniFaces (请注意,您至少需要 OmniFaces 1.6),或使用 @ViewScoped from MyFaces CODI .

相关问答:

关于java - session 范围 Bean 不是 session 范围 Bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20625488/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com