4

我是 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";
        }
     }
}
4

2 回答 2

2

调用new UserFacade()只会做它在 Java 中总是做的事情:调用类的无参数构造函数UserFacade

要获得对 EJB 的引用,您需要将其注入托管 bean,或者使用 JNDI 检索对 EJB 的引用。

于 2012-10-21T15:40:40.753 回答
2

在managedBean中使用DI注入ejb,不要在mb的构造函数中创建实例:

@ManagedBean @SessionScoped 
public class userBean {
    @EJB
    UserFacade  userFacade;
}
于 2012-10-21T15:43:31.560 回答