gpt4 book ai didi

java - JSF + JPA(PrimeFaces 向导组件)

转载 作者:行者123 更新时间:2023-12-02 07:12:23 26 4
gpt4 key购买 nike

我正在学习/培训 JSP,并决定向 PrimeFaces Wizard 组件添加持久性 ( 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

我认为这是一个简单的问题,但由于这对我来说是个新问题,而且我一直在寻找这个答案,但没有成功,所以我请求您的帮助。

这是托管Bean:

@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();
}
}
}

感谢您的帮助。

最佳答案

此行上的 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 在请求映射中可用?这就是我寻找问题的地方。

关于java - JSF + JPA(PrimeFaces 向导组件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15347978/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com