使用 CDI(上下文和依赖注入)支持 Bean 代替托管 Bean
我认为建议使用 CDI bean 作为支持 bean,而不是 JSF 托管 bean。
所以我决定创建一个小例子,以了解它是如何工作的,对于 @RequestScopedBean:
-而不是使用 @ManagedBean("beanName") ,我使用 >@Named("beanName")
- 我不使用 javax.faces.bean.RequestScopped,而是使用 javax.enterprise.context.RequestScoped;
演示程序很简单,我有一个字段和一个提交按钮,当用户输入内容并且刷新页面时,输入的值不再显示(它在请求持续时持续显示,对吧?)。我认为我做的一切都很好,但我得到一个例外:
警告:StandardWrapperValve[面孔 Servlet]:PWC1406:Servlet.service() 对于 servlet Faces Servlet 抛出 例外 javax.el.PropertyNotFoundException: /index.xhtml @19,47 value="#{cdiBean.passedValue}":目标 无法访问,标识符“cdiBean” 解析为 null
这是我的程序的样子:
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;
}
}
- 我的错误在哪里?
- 使用这种方法有什么好处?我还是不明白。
I rode that is recommended to use CDI beans as backing beans instead of JSF managed beans.
So i decided to create a little example, to understand how it works, for a @RequestScopedBean:
-instead of using @ManagedBean("beanName") ,i use @Named("beanName")
-instead of using javax.faces.bean.RequestScopped i use javax.enterprise.context.RequestScoped;
The demo program is very simple, i have a field and a submit button, when the user inputs something and the page is refreshed, the inputed value is not displayed anymore(It last while the request lasts right?). I think i did everything ok, but i get an exception that says:
WARNING: StandardWrapperValve[Faces
Servlet]: PWC1406: Servlet.service()
for servlet Faces Servlet threw
exception
javax.el.PropertyNotFoundException:
/index.xhtml @19,47
value="#{cdiBean.passedValue}": Target
Unreachable, identifier 'cdiBean'
resolved to null
This is how my program looks like:
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;
}
}
-Where is my mistake?
-What is the advantage of using this approach? I still don't understand that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
web.xml
中是否有空的beans.xml
?我认为去那里是必须的。请阅读此处第 15.6 节。摘自其中:
Do you have an empty
beans.xml
along with yourweb.xml
? I think it is mandatory to be there.Read section 15.6 here. Quote from it: