Question

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
Was it helpful?

Solution

I don't have enough rep to add a comment and ask you some questions yet, so i'll post my comment as an answer. You said that you are using glassfish3 and jsf2.2. Glassfish3 runs on javaee6 and jsf2.2 is javaee7 specific. I suggest moving to glassfish4 and javaee7, or if you are dependent on glassfish 3, downgrade to jsf2.0 and javaee6.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top