简体   繁体   English

jsf托管Bean中的EJB注入错误

[英]EJB injection in jsf managed bean error

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. 我正在使用jsf,ejb和jsf managebean。 i am injecting the ejb to the managed bean but it seems that the ejb is not injected. 我正在将ejb注入托管bean,但似乎未注入ejb。 heres my code: 这是我的代码:

jsf managed bean: jsf托管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: 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: jsf形式:

<h:form>
  <h:inputText value="#{inputBean.myValue}"></h:inputText>
  <h:commandButton value="Submit"
                             action="#{inputBean.getList}"/>
</h:form>

jsf dataTablepage: 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. 在以上代码中,总是调用getList方法的else条件,这意味着未正确注入EJB。

additional info that may help: 可能会帮助您的其他信息:

  • i am using glassfish 3.1.1 with jsf 2.2. 我正在使用带有jsf 2.2的glassfish 3.1.1。
  • I have tried using @Named annotation instead of the @ManagedBean annotation but i am getting a target unreachable exception. 我尝试使用@Named注释而不是@ManagedBean注释,但是我遇到了目标无法到达的异常。
  • I have tried injecting the EJB in a servlet and it works fine 我尝试将EJB注入到servlet中,并且工作正常

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和jsf2.2。 Glassfish3 runs on javaee6 and jsf2.2 is javaee7 specific. Glassfish3在javaee6上运行,而jsf2.2是javaee7特有的。 I suggest moving to glassfish4 and javaee7, or if you are dependent on glassfish 3, downgrade to jsf2.0 and javaee6. 我建议改用glassfish4和javaee7,或者如果您依赖于glassfish 3,则降级到jsf2.0和javaee6。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM