Problem with the ajax PrimeFaces

UI Components for JSF
Erickfm8
Posts: 56
Joined: 22 Jun 2010, 14:25

03 Mar 2011, 20:07

Good afternoon everyone.
I'm having trouble with AJAX prime, is as follows:

Every time we use a feature of prime it generates the following error:

Code: Select all

javax.faces.FacesException: Unexpected error restoring state for component with id j_idt3.  Cause: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0.
	at com.sun.faces.application.view.StateManagementStrategyImpl$1.visit(StateManagementStrategyImpl.java:273)
	at com.sun.faces.component.visit.FullVisitContext.invokeVisitCallback(FullVisitContext.java:151)
	at javax.faces.component.UIComponent.visitTree(UIComponent.java:1485)
	at javax.faces.component.UIComponent.visitTree(UIComponent.java:1496)
	at com.sun.faces.component.visit.VisitUtils.doFullNonIteratingVisit(VisitUtils.java:75)
	at com.sun.faces.application.view.StateManagementStrategyImpl.restoreView(StateManagementStrategyImpl.java:257)
	at com.sun.faces.application.StateManagerImpl.restoreView(StateManagerImpl.java:181)
	at com.sun.faces.application.view.ViewHandlingStrategy.restoreView(ViewHandlingStrategy.java:123)
	at com.sun.faces.application.view.FaceletViewHandlingStrategy.restoreView(FaceletViewHandlingStrategy.java:448)
	at com.sun.faces.application.view.MultiViewHandler.restoreView(MultiViewHandler.java:148)
	at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:187)
	at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
	at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:111)
	at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
	at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:541)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:383)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:288)
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
	at java.util.ArrayList.RangeCheck(Unknown Source)
	at java.util.ArrayList.get(Unknown Source)
	at javax.faces.component.AttachedObjectListHolder.restoreState(AttachedObjectListHolder.java:165)
	at javax.faces.component.UIComponentBase.restoreState(UIComponentBase.java:1432)
	at com.sun.faces.application.view.StateManagementStrategyImpl$1.visit(StateManagementStrategyImpl.java:265)
	... 29 more
Example:

First example
When using op: commandLink (Prime) instead of h: commandLink (JSF) generates this error.
Second example
By clicking on the prime line of the DataTable and call a dialog also generates this error

my code

cadastroCliente.xhml

Code: Select all

<?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:p="http://primefaces.prime.com.tr/ui"
	xmlns:f="http://java.sun.com/jsf/core">
<h:head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
	<title>Cadastro de Clientes</title>
</h:head>
<f:view>
	<h:body>
		<h:form id="frmGridCliente">
			<p:growl id="menssagensCliente"
					 life="3000"/>  
			<p:dataTable widgetVar="dtClientes"
						 var="cliente" 
						 value="#{mbCliente.clientes}" 
						 paginator="true"
						 rows="5" 
						 selection="#{mbCliente.cliente}" 
						 selectionMode="single"
						 onselectComplete="dlgCliente.show();"
				         emptyMessage="Nao existe Cliente cadastrado"
				         style="width:70%"
				         update="frmCadastroCliente">
	 			<p:column sortBy="#{cliente.id}" 
				          filterBy="#{cliente.id}"
				          style="width:20%">
					<f:facet name="header">
						<h:outputText value="Id" />
					</f:facet>
					<h:outputText value="#{cliente.id}" />
				</p:column>
	 			<p:column sortBy="#{cliente.nome}" 
				          filterBy="#{cliente.nome}">
					<f:facet name="header">
						<h:outputText value="Nome" />
					</f:facet>
					<h:outputText value="#{cliente.nome}" />
				</p:column>
	 			<p:column sortBy="#{cliente.cnpj}" 
					      filterBy="#{cliente.cnpj}">
					<f:facet name="header">
						<h:outputText value="Cnpj" />
					</f:facet>
					<h:outputText value="#{cliente.cnpj}" />
				</p:column>
			</p:dataTable>
			<p:panel style="width:69.2%">
				<p:commandLink title="Novo"
				               oncomplete="dlgCliente.show();"
				               action="#{mbCliente.incluir}"
				               update="frmCadastroCliente">
					<h:graphicImage url="/imagens/novo.jpg"
									style="border:0"/>
				</p:commandLink>
				<h:commandLink title="Apagar"
			               actionListener="#{mbCliente.apagar}"
			               style="margin-left:20px"
			               update="frmGridCliente">
					<p:graphicImage url="/imagens/apagar.jpg"
									style="border:0"/>
				</h:commandLink>
				<h:commandLink title="Voltar"
			      	         action="#{mbCliente.voltar}"
			    	           ajax="false"
			        	       style="margin-left:20px">
					<h:graphicImage url="/imagens/voltar.jpg"
									style="border:0"/>
				</h:commandLink>
			</p:panel>
		</h:form>		
		<h:form id="frmCadastroCliente">
			<p:dialog id="dlgCliente"
					  widgetVar="dlgCliente"
		       		  header="Cliente"
					  width="500"> 
				<h:panelGrid columns="2" id="pnl" >
					<h:outputLabel value="Id" 
		   			   	           for="itxId" 
		   			   	           rendered="#{!empty mbCliente.cliente.id}"/>
					<p:inputText id="itxId"
							     value="#{mbCliente.cliente.id}" 
							     disabled="true"
							     rendered="#{!empty mbCliente.cliente.id}"/>
					<h:outputLabel value="Nome" 
		   			   	           for="itxNome" />
					<p:inputText id="itxNome"
							     value="#{mbCliente.cliente.nome}" 
							     size="35"
							     required="true"
							     requiredMessage="Informe o Nome"/>
					<h:outputLabel value="CNPJ" 
		   			   	           for="mskCnpj" />
					<p:inputMask id="mskCnpj"
								 mask="99.999.999/9999-99"
							     value="#{mbCliente.cliente.cnpj}"
							     required="true"
							     requiredMessage="Informe o CNPJ"/>
				</h:panelGrid>
				<h:panelGrid columns="3">
					<p:commandLink title="Gravar"
				                   action="#{mbCliente.gravar}"
				                   rendered="#{empty mbCliente.cliente.id}"
				                   update="menssagensCliente, itxId, itxNome, mskCnpj, frmGridCliente">
						<p:graphicImage url="/imagens/gravar.jpg"
									style="border:0"/>
					</p:commandLink>
					<p:commandLink title="Alterar"
				                   actionListener="#{mbCliente.alterar}"
				                   rendered="#{!empty mbCliente.cliente.id}"
				                   oncomplete="dlgCliente.hide();"
				                   update="menssagensCliente, itxId, itxCnpj, itxNome, frmGridCliente">
						<p:graphicImage url="/imagens/gravar.jpg"
									style="border:0"/>
					</p:commandLink>
				</h:panelGrid>
			</p:dialog>
		</h:form>
	</h:body>
</f:view>
</html>

mbCliente.xhtml

Code: Select all

package controladores;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;

import org.primefaces.event.RowEditEvent;
import org.primefaces.event.SelectEvent;

import negocios.ClienteBO;
import entidades.Cliente;


@ViewScoped
@ManagedBean(name="mbCliente")
public class MBCliente implements Serializable {
	
	private static final long serialVersionUID = 4718957167100835066L;
	private ClienteBO clienteBO;
	public Cliente cliente;
	private List<Cliente> clientes;
		
	
	public MBCliente(){
		this.setClienteBO(new ClienteBO());
	    this.setCliente(new Cliente());
		this.setClientes(this.listar());
	}

	public ClienteBO getClienteBO() {
		return clienteBO;
	}

	public void setClienteBO(ClienteBO clienteBO) {
		this.clienteBO = clienteBO;
	}

	public Cliente getCliente() {
		return cliente;
	}

	public void setCliente(Cliente cliente) {
		this.cliente = cliente;
	}

	public List<Cliente> getClientes() {
		return clientes;
	}

	public void setClientes(List<Cliente> clientes) {
		this.clientes = clientes;
	}
	
	public void incluir(){
		this.setCliente(new Cliente());
	}
	
	public void gravar(){
		try {
			this.getClienteBO().gravar(this.getCliente());
			this.getClientes().add(this.getCliente());
			this.setCliente(new Cliente());
			JSFUtil.enviarMenssagemSucesso("Cliente gravado com sucesso");
		} catch (Exception e) {
			JSFUtil.enviarMenssagemErro("Erro ao gravar"+" "+e.getMessage() );
		}
	}
	
	public void alterar(ActionEvent evento) {
		try {
			Integer indice = this.getClientes().indexOf(this.getCliente());
			this.getClienteBO().alterar(this.getCliente());
			this.getClientes().remove(this.getCliente());
			this.getClientes().add(indice, this.getCliente());
			JSFUtil.enviarMenssagemSucesso("Cliente alterado com sucesso");
		} catch (Exception e) {
			JSFUtil.enviarMenssagemErro("Erro ao alterar"+" "+e.getMessage() );
		}
	}

	
	public void apagar(ActionEvent evento) {
		if(this.getCliente() == null || (this.getCliente() != null && this.getCliente().getId() == null) ) {
			JSFUtil.enviarMenssagemAtencao("Selecione um cliente na grid");
		}else {
			try {
				this.getClienteBO().apagar(this.getCliente());
				this.getClientes().remove(this.getCliente());
				JSFUtil.enviarMenssagemSucesso("Cliente apagado com sucesso");
			} catch (Exception e) {
				JSFUtil.enviarMenssagemErro("Erro ao apagar"+" "+e.getMessage() );
			}
		}
	}
	
	public List<Cliente> listar(){ 
		List<Cliente> clientes = new ArrayList<Cliente>();
		try {
			clientes = this.getClienteBO().listar();
		} catch (Exception e) {
			JSFUtil.enviarMenssagemErro("Erro ao Listar"+" "+e.getMessage() );
		}
		return clientes;
	}
			
	
	public String voltar(){
		return "index";
	}
}


That is any prime AJAX request generates this error.
Have googled and no surface not find solution.

I hope to help the community.
Thanks

healeyb
Posts: 365
Joined: 07 Apr 2010, 16:05

04 Mar 2011, 00:30

You are using multiple forms and will need to use something like
h:commandLink update="formid:itxId".

Regards,
Brendan.

Erickfm8
Posts: 56
Joined: 22 Jun 2010, 14:25

04 Mar 2011, 14:19

Good morning, first I want to thank the help.

Even putting in the update of DataTable (frmCadastroCliente: pnl)

By the time you click generates the same error.

healeyb
Posts: 365
Joined: 07 Apr 2010, 16:05

04 Mar 2011, 18:52

At a guess I'd say that the IndexOutOfBoundsException is coming from
ArrayList.get() (called from remove()), and something isn't happening
in the order you expect. Put some logging on your setters and getters
(or breakpoints) to get a better idea of what's going on, and in what
order.

Erickfm8
Posts: 56
Joined: 22 Jun 2010, 14:25

04 Mar 2011, 20:24

I credit other than that, I took the test and continues to remove the same error.

I believe it is something of prime

healeyb
Posts: 365
Joined: 07 Apr 2010, 16:05

04 Mar 2011, 20:44

The thing to bear in mind is that commandLinks in dataTables is a very
common case and works for me and thousands of others. It's certainly
possible that you have found an unusual situation that is causing this
problem within primefaces but it's not high on my list of likely causes.

Which commandLink are you clicking to cause this problem to occur?
Can you describe from when the page initially loads to when the error
is issued exactly what you're doing?

Erickfm8
Posts: 56
Joined: 22 Jun 2010, 14:25

09 Mar 2011, 13:37

Clicking the commLink "NOVO"or whatever, it generated the error.
Already moved to commandButton and also generates the error.

Note "The prime components of an AJAX request do everything, so if I put an h: commandLink (without ajax) JSF
anywhere on the page and click it there it works (the components of prime works)
ie if I do something that is not an AJAX request before using an ajax works "

This is very interesting

healeyb
Posts: 365
Joined: 07 Apr 2010, 16:05

09 Mar 2011, 14:53

This is a known bug in the PF 2.2 release but is fixed in 2.2.1, you
don't say which version you are using.

Erickfm8
Posts: 56
Joined: 22 Jun 2010, 14:25

17 Mar 2011, 14:22

"
"This is a known bug in the 2.2 release But PF is fixed in 2.2.1, you
Which version do not say you are using.
I switched the jars to version 2.2.1, really no longer generates this error, but the components behave erratically.
For example, if I click on the row of the table and call the "Dialog.show ()" it opens all the record that has table, or several "Dialog" instead of one.

see my code

Code: Select all

<?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:p="http://primefaces.prime.com.tr/ui"
	xmlns:f="http://java.sun.com/jsf/core">
<h:head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
	<title>Cadastro de Usuários</title>
</h:head>
<f:view>
	<h:body>
		<h:form id="frmGridUsuario">
			<p:growl id="menssagensUsuario"
					 life="3000"/>  
			<p:dataTable widgetVar="dtUsuario"
						 var="usuario" 
						 value="#{usuarioMB.usuarios}" 
						 paginator="true"
						 rows="5" 
						 selection="#{usuarioMB.usuario}" 
						 selectionMode="single"
						 onRowSelectComplete="dlgUsuario.show();"
				         emptyMessage="Nao existe Usuário cadastrado"
				         style="width:70%"
				         update="frmCadastroUsuario">
	 			<p:column sortBy="#{usuario.email}" 
					      filterBy="#{usuario.email}">
					<f:facet name="header">
						<h:outputText value="Email" />
					</f:facet>
					<h:outputText value="#{usuario.email}" />
				</p:column>
				<p:column sortBy="#{usuario.cpf}" 
					      filterBy="#{usuario.cpf}">
					<f:facet name="header">
						<h:outputText value="CPF" />
					</f:facet>
					<h:outputText value="#{usuario.cpf}" />
				</p:column>
	 			<p:column sortBy="#{usuario.nome}" 
				          filterBy="#{usuario.nome}">
					<f:facet name="header">
						<h:outputText value="Nome" />
					</f:facet>
					<h:outputText value="#{usuario.nome}" />
				</p:column>
	 			<p:column sortBy="#{usuario.perfil}" 
					      filterBy="#{usuario.perfil}">
					<f:facet name="header">
						<h:outputText value="Perfil" />
					</f:facet>
					<h:outputText value="#{usuario.perfil}" />
				</p:column>
	 		</p:dataTable>
			<p:panel style="width:69.2%">
				<p:commandLink title="Incluir"
				               oncomplete="dlgUsuario.show();"
				               action="#{usuarioMB.incluir}"
				               update="dlgUsuario">
					<h:graphicImage url="/imagens/novo.jpg"
									style="border:0"/>
				</p:commandLink>
				<p:commandLink title="Apagar"
			               actionListener="#{usuarioMB.apagar}"
			               style="margin-left:20px"
			               update="frmGridUsuario">
					<p:graphicImage url="/imagens/apagar.jpg"
									style="border:0"/>
				</p:commandLink>
				<h:commandLink title="Voltar"
			      	           action="#{usuarioMB.voltar}"
			    	           ajax="false"
			        	       style="margin-left:20px">
					<h:graphicImage url="/imagens/voltar.jpg"
									style="border:0"/>
				</h:commandLink>
			</p:panel>
		</h:form>		
		<h:form id="frmCadastroUsuario">
			<p:dialog id="dlgUsuario"
					  widgetVar="dlgUsuario"
		       		  header="Usuario"
					  width="500"
					  modal="true"> 
				<h:panelGrid columns="2">
					<h:outputLabel value="Nome" 
		   			   	           for="itxNome" />
					<p:inputText id="itxNome"
							     value="#{usuarioMB.usuario.nome}" 
							     size="35"
							     required="true"
							     requiredMessage="Informe o Nome"/>
					<h:outputLabel value="CPF" 
		   			   	           for="mskCpf" />
					<p:inputMask id="mskCpf"
							     mask="999.999.999-99"
							     value="#{usuarioMB.usuario.cpf}"							     
							     required="true"
							     requiredMessage="Informe o Cpf"/>
					<h:outputLabel value="Perfil" 
		   			   	           for="somPerfil" />
					<h:selectOneMenu id="somPerfil"
						     		 value="#{usuarioMB.usuario.perfil}"
						     		 required="true"
						      	   requiredMessage="Informe o Perfil" >
						<f:selectItems value="#{usuarioMB.perfils}"/>
					</h:selectOneMenu>
					<h:outputLabel value="Email" 
		   			   	           for="itxEmail"/>
					<p:inputText id="itxEmail"
							     value="#{usuarioMB.usuario.email}" 
							     size="35"
							     required="true"
							     requiredMessage="Informe o Email"/>
					<h:outputLabel value="Senha" 
		   			   	           for="pswSenha" 
		   			   	           rendered="#{empty usuarioMB.usuario.cpf}"/>
					<p:password  id="pswSenha"
							     value="#{usuarioMB.usuario.senha}" 
							     required="true"
							     requiredMessage="Informe a Senha"
							     rendered="#{empty usuarioMB.usuario.cpf}"/>
					<h:outputLabel value="Confirmar Senha" 
		   			   	           for="pswConfirmarSenha" 
		   			   	           rendered="#{empty usuarioMB.usuario.cpf}"/>
					<p:password  id="pswConfirmarSenha"
							     value="#{usuarioMB.senha}" 
							     required="true"
							     requiredMessage="Informe a Confirmação da Senha"
							     rendered="#{empty usuarioMB.usuario.cpf}"/>
				</h:panelGrid>
				<h:panelGrid columns="2">
					<p:commandLink title="Gravar"
				                   action="#{usuarioMB.gravar}"
				                   rendered="#{empty usuarioMB.usuario.cpf}"
				                   update="menssagensUsuario, itxId, itxNome, mskCpf, somPerfil,itxEmail ,pswSenha, pswConfirmarSenha, frmGridUsuario">
						<p:graphicImage url="/imagens/gravar.jpg"
									style="border:0"/>
					</p:commandLink>
					<p:commandLink title="Alterar"
				                   actionListener="#{usuarioMB.alterar}"
				                   rendered="#{!empty usuarioMB.usuario.cpf}"
				                   oncomplete="dlgUsuario.hide();"
				                   update="menssagensUsuario, itxId, itxNome, mskCpf, somPerfil,itxEmail ,pswSenha, pswConfirmarSenha, frmGridUsuario">
						<p:graphicImage url="/imagens/gravar.jpg"
									style="border:0"/>
					</p:commandLink>
					<p:commandLink title="Alterar Senha"
				                   rendered="#{!empty usuarioMB.usuario.cpf}"
				                   style="margin-left:20px">
				         <p:graphicImage url="/imagens/alterarSenha.jpg"
									style="border:0"/>
					</p:commandLink>
				</h:panelGrid>
			</p:dialog>
		</h:form>
	</h:body>
</f:view>
</html>

healeyb
Posts: 365
Joined: 07 Apr 2010, 16:05

17 Mar 2011, 15:51

You should use onRowSelectUpdate instead of update, this needs to update
the contents of the dialog, not the dialog itself.

So have:

Code: Select all

<p:dataTable onRowSelectUpdate="display">
<p:dialog>
  <h:panelGroup id="display">
I'm don't really see the need for a second form, why not just move the
dialog into the first form, although this may not make any difference.

http://www.primefaces.org/showcase/ui/d ... nstant.jsf

Have you bought a 2.2 user guide? it's very little money and well worth it.

Regards,
Brendan.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 29 guests