【发布时间】:2011-09-28 17:22:38
【问题描述】:
我认为建议使用 CDI bean 作为支持 bean 而不是 JSF 托管 bean。
所以我决定为 @RequestScopedBean 创建一个小示例,以了解其工作原理:
-我不使用 @ManagedBean("beanName") ,而是使用 @Named("beanName")
-而不是使用 javax.faces.bean.RequestScopped 我使用 javax.enterprise.context.RequestScoped;
演示程序非常简单,我有一个字段和一个提交按钮,当用户输入内容并刷新页面时,输入的值不再显示(它会在请求持续时持续,对吗?)。我认为我做的一切都很好,但我得到一个例外:
警告:StandardWrapperValve[Faces Servlet]:PWC1406:Servlet.service() 为 servlet Faces Servlet 抛出 例外 javax.el.PropertyNotFoundException: /index.xhtml @19,47 value="#{cdiBean.passedValue}": 目标 无法访问,标识符“cdiBean” 解决为空
这就是我的程序的样子:
index.xhtml
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>RequestScope demo CDI(Component Dependency Injection)</title>
</h:head>
<h:body>
<h:form>
<h3>RequestScope demo CDI(Component Dependency Injection)</h3>
<h:inputText value="#{cdiBean.passedValue}"/>
<br/>
<h:commandButton value="submit" action="index"/>
</h:form>
</h:body>
</html>
DemoBB.java
package backingbeans;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
@Named("cdiBean")//The Named anotation indicates that this is a CDI bean
@RequestScoped//If we use CDI beans the @RequestScoped annotation must come from: javax.enterprise.context.RequestScoped;
public class DemoBB {
//This value will be saved on the session only until the server responds to the request
private String passedValue;
public String getPassedValue() {
return passedValue;
}
public void setPassedValue(String passedValue) {
this.passedValue = passedValue;
}
}
-我的错误在哪里?
-使用这种方法有什么好处?我还是不明白。
【问题讨论】:
标签: java jsf jakarta-ee jsf-2 cdi