java – 使用CDI(上下文和依赖注入)支持bean而不是托管Bean
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 使用CDI(上下文和依赖注入)支持bean而不是托管Bean,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2663字,纯文字阅读大概需要4分钟。
内容图文
我骑了,建议使用CDI bean作为支持bean而不是JSF托管bean.
所以我决定为@RequestScopedBean创建一个小例子,以了解它是如何工作的:
– 不使用@ManagedBean(“beanName”),我使用@Named(“beanName”)
– 而不是使用javax.faces.bean.RequestScopped我使用javax.enterprise.context.RequestScoped;
演示程序非常简单,我有一个字段和一个提交按钮,当用户输入内容并刷新页面时,输入的值不再显示(请求持续时间最后一次?).我想我做的一切都好,但我得到一个例外,说:
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
这是我的程序的样子:
的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;
}
}
– 我的错误在哪里?
– 使用这种方法有什么好处?我还是不明白.
解决方法:
你有一个空的beans.xml和你的web.xml吗?我认为必须在那里.
阅读第15.6 here节.引用它:
CDI doesn’t define any special
deployment archive. You can package
beans in JARs, EJB-JARs or WARs—any
deployment location in the application
classpath. However, the archive must
be a “bean archive”. That means each
archive that contains beans must
include a file named beans.xml in the
META-INF directory of the classpath or
WEB-INF directory of the web root (for
WAR archives). The file may be empty.
Beans deployed in archives that do not
have a beans.xml file will not be
available for use in the application.In an embeddable EJB container, beans
may be deployed in any location in
which EJBs may be deployed. Again,
each location must contain a beans.xml
file.
内容总结
以上是互联网集市为您收集整理的java – 使用CDI(上下文和依赖注入)支持bean而不是托管Bean全部内容,希望文章能够帮你解决java – 使用CDI(上下文和依赖注入)支持bean而不是托管Bean所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。