我正在使用 Netbeans 开发 JavaEE 应用程序。实体管理器在应用程序中得到一个空值。em 没有被注入。仍然收到 nullPointerException,因为 em 在 createNamedQuery 中获得空值。谁能让我知道我错过了什么。
JSP 页面-index.jsp:
<%@page import="managed.userBean" %>
<jsp:useBean id="bean" class="managed.userBean" scope="session" />
.....
<input type="submit" value="Submit" onclick="<jsp:scriptlet> bean.validate();</jsp:scriptlet>" />
用户Bean.java
package managed;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import session.UserFacade;
@ManagedBean
@SessionScoped
public class userBean implements Serializable {
private String uid, password,role,fname,lname;
private String response="" ;
@EJB
UserFacade userFacade;
....
public String validate() {
System.out.println("in validate going to user facade: " + uid + password);
response = userFacade.validateUser(uid,password);
.....
}
UserFacade.java
package session;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class UserFacade extends AbstractFacade<User> {
@PersistenceContext (unitName = "ExamSysPU")
public EntityManager em;
String role;
@Override
protected EntityManager getEntityManager() {
System.out.println("check if em is open");
return em;
}
public UserFacade() {
super(User.class);
}
public String validateUser(String uid, String password) {
...
List results = em.createNamedQuery("User.findByUid").setParameter("uid", uid).getResultList();
....
}
持久性.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="ExamSysPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>test</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
谢谢, 希尔帕