我是 Java EE 的新手,并且已经尝试解决这个问题将近一周。这是我的会话 bean 中的代码,我在其中获得了NullPointerException
. 发现实体管理器没有被注入。
来自会话 bean 的代码:
package session;
import entity.User;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class UserFacade extends AbstractFacade<User> {
@PersistenceContext (unitName = "EXAMPU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public UserFacade() {
super(User.class);
}
public String validateUser(String username, String password) {
System.out.println("in facade");
boolean pwdMatch = false;
System.out.println("in UserInfoFacade validate " + em);
try {
System.out.println("username " + username);
System.out.println("password " + password);
if(em == null) {
System.out.println("em is null");
}
else {
System.out.println("em is not null");
}
List results = em.createNamedQuery("User.findByUserName").setParameter("userName", username).getResultList();
if(results == null) {
System.out.println("results is null");
}
else {
System.out.println("results is not null");
}
System.out.println("Entity manger is open " + em.isOpen());
if (!results.isEmpty())
{
Iterator userList = results.iterator();
while (userList.hasNext())
{
User user = (User) userList.next();
if (user != null)
{
if (user.getPassword().equals(password))
{
pwdMatch = true;
}
else {
pwdMatch = false;
}
}
else {
return "NULL";
}
}
if (pwdMatch) {
return "MATCH";
}
else {
return "NO_MATCH";
}
}
else {
return "NOT_FOUND";
}
} catch (Exception ex) {
//ex.printStackTrace();
System.out.println("Caught exception in UserFacade " + ex.toString());
}
return "NULL";
}
}
持久性.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="EXAMPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>EXAM</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
托管 bean 的代码:userBean
package managed;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import session.UserFacade;
import javax.ejb.EJB;
@ManagedBean
@SessionScoped
public class userBean {
private String username, password;
private String response="" ;
private UserFacade userFacade;
public userBean() {
this.userFacade = new UserFacade();
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getResponse() {
return response;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setResponse(String response) {
this.response = response;
}
public String validate() {
System.out.println("in validate going to user facade" + username + password);
response = userFacade.validateUser(username,password);
System.out.println("validUser value " + response);
if (response.equals("MATCH")){
return "welcome.xhtml";
}
else
return "index.xhtml";
}
}
}