【问题标题】:java collection usage as map and retrieving datajava集合用作地图和检索数据
【发布时间】:2013-09-05 03:29:48
【问题描述】:
Map<String, List<OfferBean>> map = new HashMap<String, List<OfferBean>>();
            List<OfferBean> al=new ArrayList<OfferBean>();
            OfferBean of=null;
            sql="select * from catgory";
            ps1=c.prepareStatement(sql);
            ps1.execute();
            rs=ps1.getResultSet();
            if(rs.next())
            {
                System.out.println("inside loop of if");
                sql="select * from catgory";
                ps1=c.prepareStatement(sql);
                ps1.execute();
                rs=ps1.getResultSet();
                while(rs.next())
                {
                    of=new OfferBean();
                    System.out.println("inside loop of while");
                    of.setCategory(rs.getString("catgoryname"));
                    al.add(of);
                }
           map.put("key", al);

我想使用两个ArrayList 两个ArrayList 我将通过使用键放入HashMap,我想传递到jsp 侧的jsp 侧我想使用@987654327 检索数据@如何reteive它请任何人帮助我

【问题讨论】:

    标签: java jsp map arraylist jstl


    【解决方案1】:
     request.setAttribute("sampleMap", map);   
    

    试试c:forEach

     <c:forEach var="sample" items="${sampleMap}">
         Key : ${sample.key}
       <c:forEach var="list" items="${sample.value}">
              Category - ${list.category} //you can access all the values of `OfferBean`
        </c:forEach>
      </c:forEach>
    

    看看访问element of the list

    【讨论】:

    • 对不起,你能帮忙看看那个值是什么 - ${list.name} thnks
    • ${list.name} 这个示例你可以放置 OfferBean 的变量
    • @user2734962 将 ${list.name} 替换为 ${item.category}
    • 是的。我不知道 OfferBean 中的变量是什么,所以我只使用了示例的名称
    • 问题出在引用变量 ${requestScope.list!=null} 你为什么要检查上述条件。你在哪里使用java中的requestScope.list
    【解决方案2】:
    request.setAttribute("myMap", map);   
    
    <c:forEach items="${myMap}" var="mapEntry">
         key : ${mapEntry.key} 
         <c:forEach items="${mapEntry.value}" var="item">
            Category :  ${item.category}
         </c:forEach><br>
     <c:forEach>
    

    【讨论】:

      猜你喜欢
      • 2021-09-13
      • 1970-01-01
      • 1970-01-01
      • 2021-08-04
      • 2021-12-07
      • 2015-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多