看起来依赖没有被注入(inject),我的代码有什么问题吗?
页面
<p:dataTable styleClass="centralizado" id="cdt" var="c" value="#{indexBean.getCampanhas()}">
索引对象
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
@ManagedBean
@ViewScoped
public class IndexBean {
@Inject
private GerenciaContas contas;
@Inject
private GerenciaCampanhas campanhas;
@Inject
private Conversor conv;
public List<Campaign> getCampanhas() throws Exception {
return campanhas.getTodasCampanhas();
}
public GerenciaContas getContas() {
return contas;
}
public Conversor getConversor() {
return conv;
}
}
我使用的类之一:
public class GerenciaCampanhas
{
public List<Campaign> getTodasCampanhas() throws Exception {
//ordinary code
}
}
我的 Conversor 类中是否缺少任何注释? 我收到 NullPointerException,因为“GerenciaCampanhas campanhas”为空,然后在我的 indexBean 中调用方法 getCampanhas() 谢谢
最佳答案
- 将
@ManagedBean
替换为@Named
- 您不能在 JSF 托管 bean 中使用@Inject
(参见 this question - 选择除
@ViewScoped
之外的范围 - CDI 托管 bean 没有这样的范围; - 确保在您的 ejb 的 META-INF 文件夹中有
beans.xml
文件(如果它是一个企业项目,它在您的 ejb 项目中位于 META-INF 下,在你的网络项目)。
喜欢:
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import javax.inject.Inject;
import xxxx.GerenciaContas;
@Named
@SessionScoped
public class IndexBean implements Serializable {
@Inject
private GerenciaContas contas;
如果您仍需要使用 @ViewScoped
,请按照 question 中的说明将 MyFaces CODI 扩展添加到您的项目中.来自巴西的问候 =)。
关于jsf - JSF 应用程序中的 JBoss AS 7.1.1 依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14380498/