当我使用 JSTL 在 JSP 端使用时,如何分隔映射键


Map<String, List<OfferBean>> map = new HashMap<String, List<OfferBean>>();
List<OfferBean> al=new ArrayList<OfferBean>();
List<OfferBean> bl=new ArrayList<OfferBean>();
OfferBean of=null;
sql="select * from catgory";// here i'm using one table data
ps1=c.prepareStatement(sql);
ps1.execute();
rs=ps1.getResultSet();
while(rs.next())
{
    of=new OfferBean();
    of.setCategory(rs.getString("catgoryname"));
    al.add(of);
}
sql="select * from projectname where sl_no_projectname";//here on more table data
ps1=c.prepareStatement(sql);
ps1.execute();
rs=ps1.getResultSet();
while(rs.next())
{
    of=new OfferBean();
    of.setCategory(rs.getString("categoryname"));
    bl.add(of);
}
map.put("key", al); // here i'm put two table data  in the map using keys 
map.put("key1",bl);
return map;
当我使用 jstl

在 jsp 端检索时如何分隔这两个键 jstl 代码是这样的:

<c:forEach var="sample" items="${sampleMap}">
  Key : ${sample.key}
  <c:forEach var="item" items="${sample.value}">
 <option>${item.category}</option>  
  </c:forEach>
  </c:forEach> 

我想将两个表数据放在两个不同的位置。

更好的选择是将映射替换为两个单独的列表。

在服务器端

 request.setAttribute("al", al);   
 request.setAttribute("bl", bl);  
or
 request.setAttribute("al",map.get("key"));   
 request.setAttribute("bl",map.get("key1"));   

在JSP中

<c:forEach var="a" items="${al}">
     <option>${a.category}</option>  
</c:forEach>

<c:forEach var="b" items="${bl}">
     <option>${b.category}</option>  
</c:forEach>

最新更新