我正在学习/培训JSP,并决定在PrimeFaces向导组件(http://www.primefaces.org/showcase/ui/wizard.jsf)中添加一个持久性。
提交save方法时出现NullPointerException:
SEVERE: 'java.lang.NullPointerException' recebido ao invocar escuta de ação '#{userWizard.save}' para o componente 'j_idt56'
Mar 11, 2013 4:16:12 PM javax.faces.event.MethodExpressionActionListener processAction
SEVERE: java.lang.NullPointerException
at org.primefaces.wizard.UserRepository.adiciona(UserRepository.java:16)
at org.primefaces.wizard.UserWizard.save(UserWizard.java:50)
Mar 11, 2013 4:16:12 PM com.sun.faces.context.AjaxExceptionHandlerImpl log
SEVERE: JSF1073: javax.faces.event.AbortProcessingException obtido durante o processamento de INVOKE_APPLICATION 5: UIComponent-ClientId=j_idt4:j_idt56, Message=/wizard.xhtml @128,44 actionListener="#{userWizard.save}": java.lang.NullPointerException
Mar 11, 2013 4:16:12 PM com.sun.faces.context.AjaxExceptionHandlerImpl log
SEVERE: /wizard.xhtml @128,44 actionListener="#{userWizard.save}": java.lang.NullPointerException
javax.faces.event.AbortProcessingException: /wizard.xhtml @128,44 actionListener="#{userWizard.save}": java.lang.NullPointerException
我认为这是一个简单的问题,但由于这对我来说是新的,而且我一直在搜索,但未成功,对于这个答案,我会请求你的帮助。
这是managedBean:
@ManagedBean(name = "userWizard")
@SessionScoped
public class UserWizard {
private User user = new User();
private List<User> users;
private boolean skip;
private static final Logger logger = Logger.getLogger(UserWizard.class.getName());
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public boolean isSkip() {
return skip;
}
public void setSkip(boolean skip) {
this.skip = skip;
}
public void setUsers(List<User> users) {
this.users = users;
}
public void save(ActionEvent actionEvent) {
// Persist user
EntityManager manager = this.getEntityManager();
UserRepository repository = new UserRepository(manager);
repository.adiciona(this.user);
this.user = new User();
// this.users = null;
FacesMessage msg = new FacesMessage("Successful", "Welcome:" + user.getFirstname());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public List<User> getUsers() {
if (this.users == null) {
EntityManager manager = this.getEntityManager();
UserRepository repository = new UserRepository(manager);
System.out.println("**** CHAMANDO O REPOSITORIO ****");
this.users = repository.buscaTodos();
}
return this.users;
}
public String onFlowProcess(FlowEvent event) {
logger.log(Level.INFO, "Current wizard step:{0}", event.getOldStep());
logger.log(Level.INFO, "Next step:{0}", event.getNewStep());
if (skip) {
skip = false; // reset in case user goes back
return "confirm";
} else {
return event.getNewStep();
}
}
private EntityManager getEntityManager() {
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
HttpServletRequest request = (HttpServletRequest) ec.getRequest();
EntityManager manager = (EntityManager) request.getAttribute("EntityManager");
return manager;
}
}
(部分)wizard.xhtml:
持久性类:
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
public class UserRepository {
private EntityManager manager;
public UserRepository(EntityManager manager) {
this.manager = manager;
}
public void adiciona(User user) {
this.manager.persist(user);
}
@SuppressWarnings("unchecked")
public List<User> buscaTodos() {
Query query = this.manager.createQuery("select x from USUARIO x");
return query.getResultList();
}
}
过滤器类(对于EntityManager):
@WebFilter(servletNames = { "Faces Servlet" })
public class JPAFilter implements Filter {
private EntityManagerFactory factory;
public void init(FilterConfig filterConfig) throws ServletException {
this.factory = Persistence.createEntityManagerFactory("PrimeWizard-PU");
}
public void destroy() {
this.factory.close();
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
EntityManager manager = this.factory.createEntityManager();
request.setAttribute("EntityManager", manager);
manager.getTransaction().begin();
chain.doFilter(request, response);
try {
manager.getTransaction().commit();
} catch (Exception e) {
manager.getTransaction().rollback();
} finally {
manager.close();
}
}
}
感谢您的帮助。
答案 0 :(得分:2)
此行NullPointerException
上的this.manager.persist(user);
- 这表示您的EntityManager
为空。这根本与JSF无关。修复获得EntityManager
的方式,然后就可以了。
private EntityManager getEntityManager() {
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
HttpServletRequest request = (HttpServletRequest) ec.getRequest();
EntityManager manager = (EntityManager) request.getAttribute("EntityManager");
return manager;
}
您的项目中是否有任何内容使EntityManager
在请求地图中可用?那就是我在寻找问题的地方。