Estou querendo excluir um item de uma linha da minha dataTable.
Só que quando tento pegar o valor da linha selecionada, gera o seguinte erro:
22:59:32,733 Advertência [javax.enterprise.resource.webcontainer.jsf.lifecycle] (http--127.0.0.1-8080-1) #{tableBean.excluir}: java.lang.ArrayIndexOutOfBoundsException: -1: javax.faces.FacesException: #{tableBean.excluir}: java.lang.ArrayIndexOutOfBoundsException: -1
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
at javax.faces.component.UICommand.broadcast(UICommand.java:315) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:139) [jboss-as-web-7.0.2.Final.jar:7.0.2.Final]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:388) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
at org.jboss.as.web.NamingValve.invoke(NamingValve.java:57) [jboss-as-web-7.0.2.Final.jar:7.0.2.Final]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:49) [jboss-as-jpa-7.0.2.Final.jar:7.0.2.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:154) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:667) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:952) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
at java.lang.Thread.run(Thread.java:722) [:1.7.0_02]
Caused by: javax.faces.el.EvaluationException: java.lang.ArrayIndexOutOfBoundsException: -1
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
... 23 more
Caused by: java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(ArrayList.java:371) [:1.7.0_02]
at java.util.ArrayList.get(ArrayList.java:384) [:1.7.0_02]
at org.primefaces.model.LazyDataModel.getRowData(LazyDataModel.java:54) [primefaces-3.1.1.jar:]
at com.teste.TableBean.excluir(TableBean.java:72) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.7.0_02]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [:1.7.0_02]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [:1.7.0_02]
at java.lang.reflect.Method.invoke(Method.java:601) [:1.7.0_02]
at org.apache.el.parser.AstValue.invoke(AstValue.java:196) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
... 24 more
Meus arquivos são:
TableBean.java
@Component("tableBean")
@Scope(value = "session")
public class TableBean implements Serializable {
private static final long serialVersionUID = 1L;
private Cliente cliente;
@Autowired
private ClienteDAO cliDAO;
private LazyDataModel<Cliente> clientesLazyModel;
private List<Cliente> clientes;
public void listar() {
setClientes(cliDAO.listar());
carregarDataTable();
}
public void excluir() {
cliDAO.excluir(getClientesLazyModel().getRowData());
getClientes().remove(getCliente());
carregarDataTable();
}
public void carregarDataTable() {
clientesLazyModel = new LazyDataModel<Cliente>() {
private static final long serialVersionUID = 1L;
@Override
public List<Cliente> load(int first, int pageSize, String sortField,
SortOrder sortOrder, Map<String, String> filters) {
List<Cliente> data = new ArrayList<Cliente>();
for (Cliente c : getClientes()) {
boolean match = true;
for (Iterator<String> it = filters.keySet().iterator(); it
.hasNext();) {
try {
String filterProperty = it.next();
String filterValue = filters.get(filterProperty);
String fieldValue = String.valueOf(c.getClass()
.getField(filterProperty).get(c));
if (filterValue == null
|| fieldValue.startsWith(filterValue)) {
match = true;
} else {
match = false;
break;
}
} catch (Exception e) {
match = false;
}
}
if (match) {
data.add(c);
}
}
int dataSize = data.size();
this.setRowCount(dataSize);
if (dataSize > pageSize) {
try {
return data.subList(first, first + pageSize);
} catch (IndexOutOfBoundsException e) {
return data.subList(first, first
+ (dataSize % pageSize));
}
} else {
return data;
}
}
};
}
public LazyDataModel<Cliente> getClientesLazyModel() {
return clientesLazyModel;
}
public void setClientesLazyModel(LazyDataModel<Cliente> clientesLazyModel) {
this.clientesLazyModel = clientesLazyModel;
}
public List<Cliente> getClientes() {
return clientes;
}
public void setClientes(List<Cliente> clientes) {
this.clientes = clientes;
}
public Cliente getCliente() {
if (cliente == null) {
cliente = new Cliente();
}
return cliente;
}
public void setCliente(Cliente cliente) {
this.cliente = cliente;
}
}
index.xhtml
<p:dataTable id="dataTable" var="c"
value="#{tableBean.clientesLazyModel}" paginator="true"
rows="5" paginatorPosition="bottom"
emptyMessage="Nenhum registro encontrado.">
<p:column>
<f:facet name="header">
<h:outputText value="ID" />
</f:facet>
<h:outputText value="#{c.id}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Nome" />
</f:facet>
<h:outputText value="#{c.nome}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Ações" />
</f:facet>
<p:commandLink update=":form:confirmacaoExclusao"
oncomplete="confirmacaoExclusao.show()">
<h:graphicImage id="imagemExcluir" value="/imagens/excluir.png" />
<f:setPropertyActionListener value="#{c}"
target="#{tableBean.cliente}" />
</p:commandLink>
<p:tooltip for="imagemExcluir" value="Excluir" />
</p:column>
</p:dataTable>
<p:confirmDialog id="confirmacaoExclusao"
message="Exclusão"
header="Deseja excluir esse registro?" severity="alert"
widgetVar="confirmacaoExclusao">
<center>
<p:commandButton action="#{tableBean.excluir}"
oncomplete="confirmacaoExclusao.hide()"
value="Sim" update="dataTable" />
<p:spacer width="10" />
<p:commandButton oncomplete="confirmacaoExclusao.hide()"
value="Não" />
</center>
</p:confirmDialog>