【发布时间】:2014-03-09 18:46:39
【问题描述】:
我正在处理一个大型 JSF 项目,但注意到我的会话 bean 都没有保留它们的值。为了尝试找出我的错误,我使用简单的注入创建了一个测试项目,但是我仍然发现会话范围的 bean 没有保留其值。
我已通过 stackoverflow.com 搜索(并在 Google 上花了几个小时)寻找答案,但找不到答案。如果有任何帮助,我将不胜感激。
我正在使用 JSF 2.2、Netbeans 7.3.1 和 Glassfish Server 4.0
我的简单测试项目的代码如下。
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>
index.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://xmlns.jcp.org/jsf/html">
<h:head>
<title>Title</title>
</h:head>
<h:body>
<h:form>
<h:inputText value="#{bean1.title}" />
<h:commandButton action="#{bean2.test()}" />
</h:form>
</h:body>
</html>
Bean1.java
package beans;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
@Named(value = "bean1")
@SessionScoped
public class Bean1 implements Serializable {
public Bean1() {
}
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
Bean2.java
package beans;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
@Named(value = "bean2")
@RequestScoped
public class Bean2 {
public Bean2() {
}
@Inject
Bean1 b1;
public String test()
{
System.out.println(b1.getTitle());
return null;
}
}
【问题讨论】:
-
@VasilLukach 我知道
javax.faces.bean.SessionScoped是ManagedBeans 所需的导入,而命名bean(CDI bean)应该使用javax.enterprise.context.SessionScoped -
您使用的是什么版本的 JSF?您的应用程序是如何部署的?
-
@JohnAment 我正在使用 JSF 2.2、Netbeans 7.3.1 和 Glassfish Server 4.0