【发布时间】:2018-11-16 01:11:41
【问题描述】:
最近从2.2 升级到JSF 2.3,我注意到@ManagedBean 已被弃用,经过一些研究发现我应该使用CDI-2.0 托管bean 和@Named 注释。
我还将@javax.faces.bean.SessionScoped 迁移到@javax.enterprise.context.SessionScoped。
但是我注意到我的 bean 是在服务器启动时创建的!
我使用用户“X”登录并更改了 bean 中的属性。之后我用另一个浏览器登录,我希望在我的属性中找到 null,但我在另一个浏览器中找到了用户“X”的最后一次更新。
我正在使用 myFaces 2.3、omnifaces 3.1,我还在我的 tomcat 中安装了 CDI。我参考了一些博客和一些响应stackoverflow,例如:
http://balusc.omnifaces.org/2013/10/how-to-install-cdi-in-tomcat.html
Migrate JSF managed beans to CDI managed beans
这是我的主要文件:
beans.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>
pom.xml:
.....
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>${primefaces.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.primefaces.extensions</groupId>
<artifactId>all-themes</artifactId>
<version>${primefaces.all.themes}</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.omnifaces/omnifaces -->
<dependency>
<groupId>org.omnifaces</groupId>
<artifactId>omnifaces</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>net.bootsfaces</groupId>
<artifactId>bootsfaces</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.myfaces.core</groupId>
<artifactId>myfaces-api</artifactId>
<version>2.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.myfaces.core/myfaces-impl -->
<dependency>
<groupId>org.apache.myfaces.core</groupId>
<artifactId>myfaces-impl</artifactId>
<version>2.3.1</version>
</dependency>
<!-- Oracle jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.enterprise/cdi-api -->
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
<scope>provided</scope>
</dependency>
.....
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-faces</artifactId>
<version>2.4.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
我做错了吗?
【问题讨论】:
-
“我也将
@javax.enterprise.context.SessionScoped迁移到@javax.faces.bean.SessionScoped” -> 你应该反过来。 CDI 版本是javax.entrerprise.context.SessionScoped,所以这是您要使用的版本。 -
@Siliarus:很可能是错字,因为它已被纠正
-
试试stackoverflow.com/questions/32942876/…,你好像有旧的 beans.xml 定义(如果你需要的话)
-
我也在使用 Spring,所以我认为问题出在 CDI + Spring
标签: spring jsf cdi tomcat8 myfaces