2016-04-27 87 views
2

我在jsp中顯示數組列表時遇到了問題。它爲每個人重複顯示一行。 我有列的表格(電話號碼,傳真,PHONE2,電子郵件)Spring MVC正確顯示JSP中的數組列表

這裏是我的道功能:

@Override 
    public ArrayList<String> moyenCom(Authentication auth) { 
     String login=auth.getName(); 
     List list; 
     List listres = null; 
     ArrayList<String> array1=new ArrayList<String>(); 

     //ArrayList<ArrayList<String>> array=new ArrayList<ArrayList<String>>(); 

     SessionFactory factory=new Configuration().configure().buildSessionFactory(); 
     Session session=factory.openSession(); 
     session.beginTransaction(); 
     //Your operation 

     String sql1="select r.id_responsable from WEBCARE.PERSONNE p,WEBCARE.RESPONSABLE r,WBB_CLU.ABONNE_COMPTE ac,WBB_CLU.COMPTE_CLU c where p.id=r.id_responsable and r.id_abonne=ac.id_abonne and c.id_compte=ac.id_compte and c.login=:x"; 
     SQLQuery query1 = session.createSQLQuery(sql1); 
     query1.setParameter("x",login); 
     query1.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP); 
     session.getTransaction().commit(); 
     list=query1.list(); 

    for(int i=0;i<list.size();i++){ 

     String res =list.get(i).toString(); 



     String[] parts = res.split("="); 
     String part2 = parts[1]; 



     System.out.println("id du responsable est"+res); 
     System.out.println("id du responsable spliteeee est "+part2); 



     session.beginTransaction(); 
     String sql="select mc.information from WEBCARE.MOYEN_COMMUNICATION mc,WEBCARE.PERSONNE p where p.id=mc.id_categorie and mc.id_categorie=:part2 and mc.categorie=1"; 
     SQLQuery query = session.createSQLQuery(sql); 

     query.setParameter("part2",part2); 

     query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP); 
     session.getTransaction().commit(); 

     listres=query.list(); 

     for(int i1=0;i1<listres.size();i1++){ 

      String resu =listres.get(i1).toString(); 
      System.out.println("info "+resu); 
      array1.add(resu); 

    } 



     } 

    System.out.println(array1); 

    return array1; 

    } 

我的控制器:

@RequestMapping("/fichets") 
public String fichets(Model model,Authentication auth){ 

    Authentication authen = SecurityContextHolder.getContext().getAuthentication(); 
     String name = authen.getName(); //get logged in username 


model.addAttribute("moycoms",metier.moyenCom(auth)); 



return "InfoAbonneTS"; 
} 

和我的jsp頁面:

<c:forEach items="${resps}" var="resp"> 

     <tr> 


     <td id="date">${resp.nom} ${resp.prenom}</td> 
     <td>${resp.poste}</td> 
     <td>${resp.password}</td> 

    <c:forEach items="${moycoms}" var="moycom"> 
     <td>${moycom}</td> 
     </c:forEach> 

     </tr> 
    </tbody> 

    </c:forEach> 

該函數返回字段信息巫婆包含所有4通知應該在每一列中顯示每一欄。返回 的ArrayList是:

{information=}, {information=01999999999}, {information=0199999333}, {[email protected]}, {information=00 }, {information=0622355114}, {information=0588888888}, {[email protected]}, {information=00 }, {information=0111111111}, {information=0666666666}, {[email protected]} 

所以前四個信息應在每一列中顯示的每個,第二四件同樣的事情...在這個例子中,我有3人。 我無法正確顯示此幫助?

resps is for displaying the first 3 columns

回答

0

看着它看起來像地圖列表中的數據。嘗試此代碼

<c:forEach items="${moycoms}" var="moycom" varStatus="stat"> 
       <c:if test="${stat.index%4==0 && stat.index!=0}"> 
        <tr> 
       </c:if> 
        <td>${moycom['information']}</td> 
       <c:if test="${stat.index%4==0 && stat.index!=0}"> 
        </tr> 
       </c:if> 
     </c:forEach> 

stat變量用於查找當前索引。我們將基於索引添加<tr>,以便每隔四個索引完成一次。如果resp包含一些可用於查找正確用戶的標識符,則使用<c:if>進行匹配。請提供更多的信息

+0

感謝@Anand Muley的回覆,y ES RESP包含ID用於顯示四列,但我沒有得到如何可以在jsp中使用它來驗證數組列表的顯示,我嘗試了與stat不工作的代碼..我只是分開值按4排組排列列表顯示每一行。 –

+0

檢查你在字符串轉換查詢結果的代碼: ''''''''''' System.out.println(「info」+ resu); array1.add(resu);' 將其更改爲: 'Map resu =(Map)listres.get(i1); array1.add(resu);' –

+0

是的,我已經刪除了查詢中的行,希望將結果轉換爲映射,我現在不需要轉換它。問題是顯示在jsp @Anand Muley –

0

只是添加 「moycoms」 屬性模型對象

,但你從

<c:forEach items="${resps}" var="resp"> 

JSTL開始您不添加任何 「resps」您的模型對象中的參數

+0

是的,我只是沒有過去 –