作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用注解@Inject 或@Produce 在ManagedBean 中注入(inject)一个CDI Bean。我使用的 CDI Bean 是:
@Named
@Startup
@ApplicationScoped
public class BaseBean {
private List<String> custs;
public List<String> getCusts() {
return custs;
}
public void setCusts(List<String> emps) {
this.custs = emps;
}
public BaseBean(){
}
@PostConstruct
void init() {
custs = new ArrayList<String>();
custs.add("Cust1");
custs.add("Cust3");
custs.add("Cust2");
custs.add("Cust4");
}
}
@SessionScoped
@ManagedBean
public class Hello implements Serializable {
@Inject
private BaseBean dBean;
private static final long serialVersionUID = 1L;
private List<String> customers;
private List<String> customersSelect;
public Hello() {
}
@PostConstruct
void init() {
// dBean = new BaseBean();
customers = dBean.getCusts();
}
public List<String> getCustomers() {
return customers;
}
public List<String> getCustomersSelect() {
return customersSelect;
}
public void setCustomersSelect(List<String> customersSelect) {
this.customersSelect = customersSelect;
}
}
<h:form id ="f">
<h:selectManyCheckbox layout="pageDirection" border="1" value="#{hello.customersSelect}">
<f:selectItems value="#{hello.customers}"></f:selectItems>
</h:selectManyCheckbox><br />
<h:commandButton action="response.xhtml" value="Click me" />
</h:form>
@SessionScoped
(
javax.enterprise.context.SessionScoped
) 和
@Named
在
Hello
类(class)。虽然我没有收到
NullPointerException
,
h:selectManyCheckbox
是空的。此外,让我印象深刻的是,当我添加
beans.xml
META-INF
下的文件文件夹,我收到一个
StartException
,虽然文件应该在那里。我认为我的应用程序缺少能够进行依赖注入(inject)的正确配置。什么可能需要额外的配置?
<?xml version="1.0" encoding="UTF-8"?>
Services which failed to start: service jboss.deployment.unit."JSF1.war".PARSE: org.jboss.msc.service.StartException in service jboss.deployment.unit."JSF1.war".PARSE: Failed to process phase PARSE of deployment "JSF1.war"
12:51:11,482 ERROR [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) {"JBAS014653: Composite operation failed and was rolled back. Steps that failed:" => {"Operation step-2" => {"JBAS014671: Failed services" => {"jboss.deployment.unit.\"JSF1.war\".PARSE" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"JSF1.war\".PARSE: Failed to process phase PARSE of deployment \"JSF1.war\""}}}}
最佳答案
如果您使用 @Named
,@patlov 的建议将起作用在您的 CDI bean 上。但是,如果您在支持 CDI 的环境中工作,不要使用 @ManagedBean
.相反,一直使用 CDI。看到这个answer而且我相信您可以找到许多其他强烈建议您不要尝试做的事情。
只需从 javax.faces.bean.SessionScoped
切换至javax.enterprise.context.SessionScoped
一切都会神奇地工作。您可能会遇到@ViewScoped
的缺失。但是,来自 CDI,在这种情况下,请使用 JBoss Seam 或 Apache Deltaspike 之类的工具来为您实现它。作为一个额外的好处,如果您已经为 JSF 编写了现有代码,它们还将自动将所有 JSF 范围替换为 CDI 范围。
更新:
这应该是 beans.xml 的内容:
<?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>
关于jsf - 如何在 ManagedBean 中注入(inject) CDI Bean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15341877/
我是一名优秀的程序员,十分优秀!