i am trying to make a sample application that shows a table of students based on a substring input by a user in a form. I am using jsf, ejb, and jsf managebean. i am injecting the ejb to the managed bean but it seems that the ejb is not injected. heres my code:
jsf managed bean:
@ManagedBean
@RequestScoped
public class InputBean {
@EJB(beanName = "sbean")
private StudentBean studentBean;
private String myValue;
private List<Student> studentList;
public String getList(){
System.out.println(this.myValue);
if(studentBean != null){
System.out.println("Student Bean NOT null");
this.studentList = studentBean.findByLastname(this.myValue);
}
else{
System.out.println("Student Bean IS null");
}
return "dataTable";
}
//setters and getters here
}
ejb:
@Stateless(name = "sbean")
@LocalBean
public class StudentBean {
@PersistenceContext
private EntityManager em;
public List<Student> findByLastname(String lastname){
List<Student> students = em.createQuery("select s from Student s where s.lastname LIKE :keyword").setParameter("keyword", lastname +"%").getResultList();
return students;
}
}
jsf form:
<h:form>
<h:inputText value="#{inputBean.myValue}"></h:inputText>
<h:commandButton value="Submit"
action="#{inputBean.getList}"/>
</h:form>
jsf dataTablepage:
<h:dataTable value="#{inputBean.studentList}" var="student">
<h:column>
<h:outputText value="#{student.studentId}"></h:outputText>
</h:column>
<h:column>
<h:outputText value="#{student.firstname}"></h:outputText>
</h:column>
<h:column>
<h:outputText value="#{student.middlename}"></h:outputText>
</h:column>
<h:column>
<h:outputText value="#{student.lastname}"></h:outputText>
</h:column>
</h:dataTable>
in the above codes, the else condition of the getList method is always invoked, which means that the EJB is not properly injected.
additional info that may help:
- i am using glassfish 3.1.1 with jsf 2.2.
- I have tried using @Named annotation instead of the @ManagedBean annotation but i am getting a target unreachable exception.
- I have tried injecting the EJB in a servlet and it works fine