2015-04-24 75 views
2

我真的有一個讓我發瘋的問題; 我想在我的數據庫中搜索一個實例,並以p:inputText的JSF形式返回此實例的所有項目。返回休眠狀態的實例的所有項目

這是我做了什麼:

這是搜索方法:

public List findByCriteria(Class clazz, Criterion critere) { 

    List objects = null; 
    try { `openCurrentSessionwithTransaction(); ` 
     Criteria crit = currentSession.createCriteria(clazz).add(critere); 

     objects = crit.list(); 

    } catch (Exception e) { 

     e.printStackTrace(); 

    } finally { 
     currentSession.close(); 
    } 
    return objects; 
} 

這是我在ClientCtr控制器類方法:

public void ChercherContact() { 
    try { 
     Client clt = new Client(); 
     System.out.println("id " + idSociete); 
     Criterion critere = Restrictions.eq("idSociete", 
     client.getIdSociete()); 
     List<Client> l = ClientDao.findByCriteria(Client.class, critere); 
     clt = l.get(0); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

這是我的表格:

<!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:ui="http://java.sun.com/jsf/facelets" 
xmlns:f="http://java.sun.com/jsf/core" 
xmlns:h="http://java.sun.com/jsf/html" 
xmlns:p="http://primefaces.org/ui"> 


<h:form id="f"> 
<h:panelGrid columns="1" style="margin: 0 auto"> 
<p:graphicImage value="ooredoo.jpg" 
style="height : 204px; width : 590px; margin: 0 auto " /> 

</h:panelGrid> 
<h:panelGrid columns="4" style="margin: 0 auto"> 
<h:outputText value="ID CLIENT:" 
style="FONT-SIZE: large; COLOR: #ff0000; FONT-WEIGHT: bold;" /> 

<p:inputText value="#{clientCtr.client.idSociete}" id="id_societe" 
style="background-color:White;font-weight:bold;border-width:medium;border-color:Red;border-style:solid;"> 
</p:inputText> 
<p:commandButton action="#{clientCtr.ChercherContact}" 
update="f" value="CHERCHER" 
style="background-color:Red;color:White;font-weight: bold;font-size: large;border-color:Black;float:center;border-style:outset; width : 176px;"> 
</p:commandButton> 
<p:commandButton id="quitter" value="QUITTER" action="e" 
onclick="window.close()" 
style="background-color:Red;color:White;font-weight: bold;font-size: large;border-color:Black;float:center;border-style:outset; width : 176px;"> 
</p:commandButton> 
</h:panelGrid> 
<h:panelGrid columns="2" style="margin: 0 auto" id="grid"> 
<h:outputText value="Nom De La Société:" 
style="FONT-SIZE: large; COLOR: #ff0000; FONT-WEIGHT: bold;" /> 
<p:inputText value="#{clientCtr.client.nomSociete}" id="nom_societe" 
style="background-color:White;font-weight: bold;border-width:medium;border-color:Red;border-style:solid;"></p:inputText> 

<h:outputText value="ID De La Société:" 
style="FONT-SIZE: large; COLOR: #ff0000; FONT-WEIGHT: bold;" /> 
<p:inputText value="#{clientCtr.client.idSociete}" id="id_societes" 
style="background-color:White;font-weight: bold;border-width:medium;border-color:Red;border-style:solid;"></p:inputText> 
<h:outputText value="Route:" 
style="FONT-SIZE: large; COLOR: #ff0000; FONT-WEIGHT: bold;" /> 
<p:inputText value="#{clientCtr.client.route}" id="route" 
style="background-color:White;font-weight: bold;border-width:medium;border-color:Red;border-style:solid;"></p:inputText> 
<h:outputText value="NCGR:" 
style="FONT-SIZE: large; COLOR: #ff0000; FONT-WEIGHT: bold;" /> 
<p:inputText value="#{clientCtr.client.ncgr}" id="ncgr" 
style="background-color:White;font-weight: bold;border-width:medium;border-color:Red;border-style:solid;"></p:inputText> 
<h:outputText value="FQDN:" 
style="FONT-SIZE: large; COLOR: #ff0000; FONT-WEIGHT: bold;" /> 
<p:inputText value="#{clientCtr.client.fqdn}" id="fqdn" 
style="background-color:White;font-weight: bold;border-width:medium;border-color:Red;border-style:solid;"></p:inputText> 
<h:outputText value="RTGRP:" 
style="FONT-SIZE: large; COLOR: #ff0000; FONT-WEIGHT: bold;" /> 
<p:inputText value="#{clientCtr.client.rtgrp}" id="rtgrp" 
style="background-color:White;font-weight: bold;border-width:medium;border-color:Red;border-style:solid;"></p:inputText> 
<h:outputText value="OUTBPROXY:" 
style="FONT-SIZE: large; COLOR: #ff0000; FONT-WEIGHT: bold;" /> 
<p:inputText value="#{clientCtr.client.outbproxy}" id="outbproxy" 
style="background-color:White;font-weight: bold;border-width:medium;border-color:Red;border-style:solid;"></p:inputText> 
<h:outputText value="Directory Number:" 
style="FONT-SIZE: large; COLOR: #ff0000; FONT-WEIGHT: bold;" /> 
<p:inputText value="#{clientCtr.client.directoryNumber}" 
id="directory_number" 
style="background-color:White;font-weight: bold;border-width:medium;border-color:Red;border-style:solid;"></p:inputText> 
<h:outputText value="NDEST:" 
style="FONT-SIZE: large; COLOR: #ff0000; FONT-WEIGHT: bold;" /> 
<p:inputText value="#{clientCtr.client.ndest}" id="ndest" 
style="background-color:White;font-weight: bold;border-width:medium;border-color:Red;border-style:solid;"></p:inputText> 
<h:outputText value="Operator Number:" 
style="FONT-SIZE: large; COLOR: #ff0000; FONT-WEIGHT: bold;" /> 
<p:inputText value="#{clientCtr.client.operatorNumber}" 
id="operateur_number" 
style="background-color:White;font-weight: bold;border-width:medium;border-color:Red;border-style:solid;"></p:inputText> 
<h:outputText value="Additional DN :" 
style="FONT-SIZE: large; COLOR: #ff0000; FONT-WEIGHT: bold;" /> 
<h:selectOneListbox value="additional_dn" 
style="background-color:White;font-weight:bold;border-color:Red;border-border-style:solid; width : 169px; " /> 

<h:outputText value="SCRI:" 
style="FONT-SIZE: large; COLOR: #ff0000; FONT-WEIGHT: bold;" /> 
<p:inputText value="#{clientCtr.client.scri}" id="scri" 
style="background-color:White;font-weight: bold;border-width:medium;border-color:Red;border-style:solid;"></p:inputText> 

<h:outputText value="UPDI:" 
style="FONT-SIZE: large; COLOR: #ff0000; FONT-WEIGHT: bold;" /> 
<p:inputText value="#{clientCtr.client.upd}" id="updi" 
style="background-color:White;font-weight: bold;border-width:medium;border-color:Red;border-style:solid;"></p:inputText> 
</h:panelGrid> 
<h:panelGrid columns="2" style="margin: 0 auto"> 


<p:commandButton id="quittere" value="QUITTER" action="e" 
onclick="window.close()" 
style="background-color:Red;color:White;font-weight: bold;font-size: large;border-color:Black;float:center;border-style:outset; width : 176px;" /> 

</h:panelGrid> 
</h:form> 
</html> 

,如果我跑的形式,當我點擊搜索按鈕,我得到這個錯誤:

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 
at java.util.ArrayList.rangeCheck(Unknown Source) 
at java.util.ArrayList.get(Unknown Source) 
at tn.ooredoo.kpi.controller.ClientCtr.ChercherContact(ClientCtr.java:210) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at org.apache.el.parser.AstValue.invoke(AstValue.java:278) 
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274) 
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102) 
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:84) 
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98) 
at javax.faces.component.UICommand.broadcast(UICommand.java:311) 
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781) 
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246) 
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77) 
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97) 
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114) 
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) 
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) 
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002) 
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585) 
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
at java.lang.Thread.run(Unknown Source) 

我測試別的東西,如果我刪除了所有的inputtext,我離開只是一個inputtext,它工作得很好,它得到了實例的正確項目。

回答

0

看起來ClientDao.findByCriteria(Client.class, critere)將返回一個空列表,從至極你試圖讓一個元素:clt = l.get(0),因此你java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

+0

我覺得沒什麼,因爲如果我在一個控制檯進行測試,當我這樣做l.size()它返回我1,這意味着有列表中的客戶端 –

+0

替換'clt = l.get(0);' 'clt =!l.isEmpty()? l.get(0):null;'並檢查你是否仍在處理這個例外。 – Szarpul

+0

Szarpul:當我按照你告訴我的方式進行替換時,它會刪除異常,但它不會以 –