簡體   English   中英

Spring MVC在JSP中正確顯示arraylist

[英]Spring MVC displaying arraylist in JSP correctly

我在jsp中顯示arraylist時遇到問題。 它為每個重復的人將所有內容顯示在一行中。 我有一個帶有列的表格(電話號碼,傳真,phone2,電子郵件)

這是我的dao函數:

@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=01234567890}, {information=01999999999}, {information=0199999333}, {information=resp1@gmail.com}, {information=00 }, {information=0622355114}, {information=0588888888}, {information=respons3@gmail.com}, {information=00 }, {information=0111111111}, {information=0666666666}, {information=responsable4@gmail.com}

因此,前四個信息應在每一列中分別顯示,后四個相同的東西……在此示例中,我有3個人。 我無法正確顯示此幫助嗎?

resps用於顯示前3列

查看數據看起來就像是地圖列表。 試試這個代碼

<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>進行匹配。 請提供其他信息

您只向模型對象添加“ moycoms”屬性

但是你的jstl從

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

您沒有在模型對象中添加任何“ resps”參數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM