我正在开发一个大型JSF项目但是注意到我的会话bean都没有保留它们的值。为了尝试找到我的错误,我创建了一个带有简单注入的测试项目,但是我仍然发现会话作用域bean没有保留其值。
我搜索了stackoverflow.com(并在Google上花了几个小时)寻找答案但找不到答案。我会非常感谢任何帮助。
我正在使用JSF 2.2,Netbeans 7.3.1& Glassfish Server 4.0
我的简单测试项目的代码如下。
<?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>
<?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>
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;
}
}
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;
}
}
答案 0 :(得分:2)
如果我不得不猜测,您的CDI 1.0 beans.xml会导致应用服务器混乱。尝试升级到CDI 1.1 beans.xml
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
</beans>