Ошибка с TreeNode (Primefaces)
Я разрабатываю систему с использованием jsf, hibernate, spring и primefaces. У меня есть один объект, который в каждой вставке, все данные представлены в TreeNode. Когда я выбираю один узел из дерева, должна появиться форма, чтобы я мог редактировать выбранный элемент. Да, это работает очень хорошо. Но есть ошибка, когда я щелкаю один раз в узле, который появляется в консоли. Если я дважды нажму, это работает. Но я хочу знать, что это за ошибка, которая появляется, когда я щелкаю один раз в узле.
Посмотрите:
Jul 19, 2013 9:37:05 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet Faces Servlet threw exception
java.lang.NullPointerException
at org.primefaces.component.tree.Tree.processUpdates(Tree.java:308)
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1289)
at javax.faces.component.UIViewRoot.processUpdates(UIViewRoot.java:1254)
at com.sun.faces.lifecycle.UpdateModelValuesPhase.execute(UpdateModelValuesPhase.java:78)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:344)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:110)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:98)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:95)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:79)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:120)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:55)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:36)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:188)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:106)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:80)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:150)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at financeiro.web.filter.ConexaoHibernateFilter.doFilter(ConexaoHibernateFilter.java:24)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:724)
Jul 19, 2013 9:39:06 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet Faces Servlet threw exception
java.lang.NullPointerException
at org.primefaces.component.tree.Tree.processUpdates(Tree.java:308)
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1289)
at javax.faces.component.UIViewRoot.processUpdates(UIViewRoot.java:1254)
at com.sun.faces.lifecycle.UpdateModelValuesPhase.execute(UpdateModelValuesPhase.java:78)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:344)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:110)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:98)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:95)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:79)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:120)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:55)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:36)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:188)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:106)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:80)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:150)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at financeiro.web.filter.ConexaoHibernateFilter.doFilter(ConexaoHibernateFilter.java:24)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:724)
Вот страница HTML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.prime.com.tr/ui">
<ui:composition template="/templates/interna.xhtml">
<ui:define name="titulo">
Categorias
</ui:define>
<ui:define name="corpo">
<f:view contentType="text/html">
<h:panelGrid columns="2" cellpadding="5">
<h:panelGroup>
<h:form>
<p:commandButton value="Novo" action="#{categoriaBean.novo}" update="edicao" />
</h:form>
<br />
<h:form id="arvore">
<p:tree value="#{categoriaBean.categoriasTree}" var="node"
dynamic="false"
selectionMode="single"
nodeSelectListener="#{categoriaBean.selecionar}"
update="edicao"
expandAnim="FADE_IN" collapseAnim="FADE_OUT">
<p:treeNode>
<h:outputText value="#{node.descricao}" />
</p:treeNode>
</p:tree>
</h:form>
</h:panelGroup>
<h:panelGroup id="edicao">
<h:messages />
<h:form rendered="#{categoriaBean.mostraEdicao}">
<h:inputHidden value="#{categoriaBean.editada.codigo}" />
<h:inputHidden value="#{categoriaBean.editada.fator}" />
<h:panelGrid columns="2">
<h:outputLabel value="Pai" />
<h:selectOneMenu value="#{categoriaBean.editada.pai}">
<f:selectItems value="#{categoriaBean.categoriasSelect}" />
</h:selectOneMenu>
<h:outputLabel value="Descrição" />
<h:inputText value="#{categoriaBean.editada.descricao}" />
<h:outputText />
<h:panelGroup>
<p:commandButton value="Salvar" action="#{categoriaBean.salvar}"
update="edicao, arvore" />
<p:commandButton value="Excluir" action="#{categoriaBean.excluir}"
update="edicao, arvore" />
</h:panelGroup>
<h:outputText />
<p:commandButton value="Nova subcategoria" action="#{categoriaBean.novo}"
update="edicao" />
</h:panelGrid>
</h:form>
</h:panelGroup>
</h:panelGrid>
</f:view>
</ui:define>
</ui:composition>
</html>
Вот класс Бин:
@ManagedBean(name = "categoriaBean")
@RequestScoped
public class CategoriaBean {
private TreeNode categoriasTree;
private Categoria editada = new Categoria();
private List<SelectItem> categoriasSelect;
private boolean mostraEdicao = false;
public void novo() {
Categoria pai = null;
if (this.editada.getCodigo() != null) {
CategoriaRN categoriaRN = new CategoriaRN();
pai = categoriaRN.carregar(this.editada.getCodigo());
}
this.editada = new Categoria();
this.editada.setPai(pai);
this.mostraEdicao = true;
}
public void selecionar(NodeSelectEvent event) {
this.editada = (Categoria) event.getTreeNode().getData();
Categoria pai = this.editada.getPai();
if (this.editada != null && pai != null && pai.getCodigo() != null) {
this.mostraEdicao = true;
}
else {
this.mostraEdicao = false;
}
}
public void salvar() {
ContextoBean contextoBean = ContextoUtil.getContextoBean();
CategoriaRN categoriaRN = new CategoriaRN();
this.editada.setUsuario(contextoBean.getUsuarioLogado());
categoriaRN.salvar(this.editada);
this.editada = null;
this.mostraEdicao = false;
this.categoriasTree = null;
this.categoriasSelect = null;
}
public void excluir() {
CategoriaRN categoriaRN = new CategoriaRN();
categoriaRN.excluir(this.editada);
this.editada = null;
this.mostraEdicao = false;
this.categoriasTree = null;
this.categoriasSelect = null;
}
public TreeNode getCategoriasTree() {
if (this.categoriasTree == null) {
ContextoBean contextoBean = ContextoUtil.getContextoBean();
CategoriaRN categoriaRN = new CategoriaRN();
List<Categoria> categorias = categoriaRN.listar(contextoBean.getUsuarioLogado());
this.categoriasTree = new DefaultTreeNode(null, null);
this.montarDadosTree(this.categoriasTree, categorias);
}
return this.categoriasTree;
}
private void montarDadosTree(TreeNode pai, List<Categoria> lista) {
if (lista != null && lista.size() > 0) {
TreeNode filho = null;
for (Categoria categoria : lista) {
filho = new DefaultTreeNode(categoria, pai);
this.montarDadosTree(filho, categoria.getFilhos());
}
}
}
public List<SelectItem> getCategoriasSelect() {
if (this.categoriasSelect == null) {
this.categoriasSelect = new ArrayList<SelectItem>();
ContextoBean contextoBean = ContextoUtil.getContextoBean();
CategoriaRN categoriaRN = new CategoriaRN();
List<Categoria> categorias = categoriaRN.listar(contextoBean.getUsuarioLogado());
this.montarDadosSelect(this.categoriasSelect, categorias, "");
}
return categoriasSelect;
}
private void montarDadosSelect(List<SelectItem> select, List<Categoria> categorias, String prefixo) {
SelectItem item = null;
if (categorias != null) {
for (Categoria categoria : categorias) {
item = new SelectItem(categoria, prefixo + categoria.getDescricao());
item.setEscape(false);
select.add(item);
this.montarDadosSelect(select, categoria.getFilhos(), prefixo + " ");
}
}
}
public Categoria getEditada() {
return editada;
}
public void setEditada(Categoria editada) {
this.editada = editada;
}
public boolean isMostraEdicao() {
return mostraEdicao;
}
public void setMostraEdicao(boolean mostraEdicao) {
this.mostraEdicao = mostraEdicao;
}
public void setCategoriasTree(TreeNode categoriasTree) {
this.categoriasTree = categoriasTree;
}
public void setCategoriasSelect(List<SelectItem> categoriasSelect) {
this.categoriasSelect = categoriasSelect;
}
}
Класс RN (из объекта бизнес-правила):
public class CategoriaRN {
private CategoriaDAO categoriaDAO;
public CategoriaRN() {
this.categoriaDAO = DAOFactory.criarCategoriaDAO();
}
public List<Categoria> listar(Usuario usuario) {
return this.categoriaDAO.listar(usuario);
}
public Categoria salvar(Categoria categoria) {
Categoria pai = categoria.getPai();
if (pai == null) {
String msg = "A Categoria " + categoria.getDescricao() + " deve ter um pai definido";
throw new IllegalArgumentException(msg);
}
boolean mudouFator = pai.getFator() != categoria.getFator();
categoria.setFator(pai.getFator());
categoria = this.categoriaDAO.salvar(categoria);
if (mudouFator) {
categoria = this.carregar(categoria.getCodigo());
this.replicarFator(categoria, categoria.getFator());
}
return categoria;
}
private void replicarFator(Categoria categoria, int fator) {
if (categoria.getFilhos() != null) {
for (Categoria filho : categoria.getFilhos()) {
filho.setFator(fator);
this.categoriaDAO.salvar(filho);
this.replicarFator(filho, fator);
}
}
}
public void excluir(Categoria categoria) {
this.categoriaDAO.excluir(categoria);
}
public void excluir(Usuario usuario) {
List<Categoria> lista = this.listar(usuario);
for (Categoria categoria : lista) {
this.categoriaDAO.excluir(categoria);
}
}
public Categoria carregar(Integer categoria) {
return this.categoriaDAO.carregar(categoria);
}
public void salvarEstruturaPadrao(Usuario usuario) {
Categoria despesas = new Categoria(null, usuario, "DESPESAS", -1);
despesas = this.categoriaDAO.salvar(despesas);
this.categoriaDAO.salvar(new Categoria(despesas, usuario, "Moradia", -1));
this.categoriaDAO.salvar(new Categoria(despesas, usuario, "Alimentação", -1));
this.categoriaDAO.salvar(new Categoria(despesas, usuario, "Vestuário", -1));
this.categoriaDAO.salvar(new Categoria(despesas, usuario, "Deslocamento", -1));
this.categoriaDAO.salvar(new Categoria(despesas, usuario, "Cuidados Pessoais", -1));
this.categoriaDAO.salvar(new Categoria(despesas, usuario, "Educação", -1));
this.categoriaDAO.salvar(new Categoria(despesas, usuario, "Saúde", -1));
this.categoriaDAO.salvar(new Categoria(despesas, usuario, "Lazer", -1));
this.categoriaDAO.salvar(new Categoria(despesas, usuario, "Despesas Financeiras", -1));
Categoria receitas = new Categoria(null, usuario, "RECEITAS", 1);
receitas = this.categoriaDAO.salvar(receitas);
this.categoriaDAO.salvar(new Categoria(receitas, usuario, "Salário", 1));
this.categoriaDAO.salvar(new Categoria(receitas, usuario, "Restituições", 1));
this.categoriaDAO.salvar(new Categoria(receitas, usuario, "Rendimento", 1));
}
}
Сопоставленный класс:
@Entity
public class Categoria implements Serializable {
private static final long serialVersionUID = -4996563889965803475L;
@Id
@GeneratedValue
private Integer codigo;
@ManyToOne
@JoinColumn(name = "categoriaPai", nullable = true)
@ForeignKey(name = "fkCategoriaCategoria")
private Categoria pai;
@ManyToOne
@JoinColumn(name = "usuario")
@OnDelete(action=OnDeleteAction.CASCADE)
@ForeignKey(name = "fkCategoriaUsuario")
private Usuario usuario;
private String descricao;
private int fator;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
@JoinColumn(name = "categoriaPai", updatable = false)
@OrderBy(value="descricao asc")
private List<Categoria> filhos;
public Categoria() { }
public Categoria(Categoria pai, Usuario usuario, String descricao, int fator) {
this.pai = pai;
this.usuario = usuario;
this.descricao = descricao;
this.fator = fator;
}
public Integer getCodigo() {
return codigo;
}
public void setCodigo(Integer codigo) {
this.codigo = codigo;
}
public Categoria getPai() {
return pai;
}
public void setPai(Categoria pai) {
this.pai = pai;
}
public Usuario getUsuario() {
return usuario;
}
public void setUsuario(Usuario usuario) {
this.usuario = usuario;
}
public String getDescricao() {
return descricao;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
public int getFator() {
return fator;
}
public void setFator(int fator) {
this.fator = fator;
}
public List<Categoria> getFilhos() {
return filhos;
}
public void setFilhos(List<Categoria> filhos) {
this.filhos = filhos;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((codigo == null) ? 0 : codigo.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Categoria other = (Categoria) obj;
if (codigo == null) {
if (other.codigo != null)
return false;
} else if (!codigo.equals(other.codigo))
return false;
return true;
}
}
И у меня есть конвертер:
@FacesConverter(forClass = Categoria.class)
public class CategoriaConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value != null && value.trim().length() > 0) {
Integer codigo = Integer.valueOf(value);
try {
CategoriaRN categoriaRN = new CategoriaRN();
return categoriaRN.carregar(codigo);
}
catch (Exception e) {
throw new ConverterException("Não foi possível encontrar a categoria de código " + value + ". " + e.getMessage());
}
}
return null;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (value != null) {
Categoria categoria = (Categoria) value;
return categoria.getCodigo().toString();
}
return "";
}
}
Эта система из книги, которую я изучаю
Если вам нужен еще один класс, просто скажите мне, что я действительно хочу понять, что происходит
Я ценю, если кто-нибудь может мне помочь.