【问题标题】:Spring MVC displaying arraylist in JSP correctlySpring MVC 在 JSP 中正确显示 arraylist
【发布时间】:2016-08-21 13:34:15
【问题描述】:

我在 jsp 中显示数组列表时遇到问题。对于每个重复的人,它将所有内容显示在一行中。 我有一个包含列的表格(电话号码、传真、电话 2、电子邮件)

这是我的 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 条信息。 返回的数组列表是:

{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 is for displaying the first 3 columns

【问题讨论】:

    标签: java jsp spring-mvc arraylist


    【解决方案1】:

    查看数据,它看起来像一个地图列表。试试这个代码

    <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 变量用于查找当前索引。我们正在根据索引添加&lt;tr&gt;,以便每隔四个索引完成一次。如果 resp 包含一些可用于查找正确用户的标识符,则使用 &lt;c:if&gt; 进行匹配。请提供更多信息

    【讨论】:

    • 感谢@Anand Muley 的回复,是的,resp 包含用于显示四列的 id,但是我不明白如何在 jsp 中使用它来验证数组列表的显示,我试过了带有 stat 的代码不起作用.. 我只需将 arraylist 的值按 4 组分隔,以在一行中显示每个值。
    • 在此处检查了将查询结果转换为字符串的代码:String resu =listres.get(i1).toString(); System.out.println("info "+resu); array1.add(resu); 将其更改为:Map resu = (Map)listres.get(i1); array1.add(resu);
    • 是的,我已经删除了查询中的行希望将结果转换为地图我现在不需要转换它。问题是在 jsp @Anand Muley 中显示
    • 如果有错误,请发布。我认为上面的代码应该可以显示与信息对应的实际值。如果显示意味着将每个用户的 4 个值组合在一起并连续显示,那么它是一个业务逻辑,您将必须比较来自 resps 的 id 以及与 moycom 匹配的东西来区分用户的一行。现在显示的怎么样了?
    【解决方案2】:

    您只向模型对象添加“moycoms”属性

    但是你jstl从

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

    您没有在模型对象中添加任何“响应”参数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-20
      • 2013-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-17
      • 2017-10-25
      • 2016-11-05
      相关资源
      最近更新 更多