[英]CDI Beans not retaining values
我正在处理一个大型的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;
}
}
如果我不得不猜测,您的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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.