我是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";
}
}
的persistence.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";
}
}
}
答案 0 :(得分:2)
调用new UserFacade()
只会执行它在Java中始终做的事情:调用UserFacade
类的no-arg构造函数。
要获取对EJB的引用,您需要将其注入托管bean,或者使用JNDI检索对EJB的引用。
答案 1 :(得分:2)
使用DI注入managedBean中的ejb,不要在mb的构造函数中创建实例:
@ManagedBean @SessionScoped
public class userBean {
@EJB
UserFacade userFacade;
}