我有一个 ManagedBean,它注入了一个无状态 bean,但它总是给我一个用于注入的空指针。我在这里做错了什么?(我正在学习 JSF,它只是一个例子,所以请忽略类的命名等)
有两个不同的罐子(web.jar 和 services.jar)
托管豆
@ManagedBean
@RequestScoped
public class HelloPB
{
@Inject
private ExamServiceBase examService;
private String name = "";
public String getName()
{
Exam exam = examService.getSingleExam();
return exam.getName();
}
public void setName(final String name)
{
//this.name = name;
}
public String getHello()
{
if ( name == null || name.length() < 1 )
{
return null;
}
return "Hello " + name;
}
}
无状态bean
@Stateless
public class ExamServiceBase implements ExamService{
@PersistenceContext(name="QuestifyPersistUnit")
private EntityManager em;
public Exam getExam(String id){
return em.find(Exam.class, id);
}
public Exam getSingleExam(){
return em.find(Exam.class, "9E69F3EE-AE9E-4D53-B531-35504EDA450F");
}
}
索引.xhtml
<h:body>
<h:outputScript name="jsf.js" library="javax.faces" target="body">
</h:outputScript>
<h1>JSF 2 Demo</h1>
<h:form>
<h:inputText id="name" value="#{helloPB.name}">
<f:ajax render="helloTextOutput" />
</h:inputText>
<h:commandButton value="Say Hi via Ajax">
<f:ajax execute="name" render="helloTextOutput" />
</h:commandButton>
<h:outputText id="helloTextOutput" value="#{helloPB.hello}" />
</h:form>
</h:body>