如何使用 JSTL 在 jsp 页面中迭代 Map<String, List> map = new HashMap<String, List>()



我将两个列表添加到一个地图地图=新的HashMap()。我想使用 JSTL 在 jsp 页面中获取值我的控制者是

     @RequestMapping("/addprogram")
 public ModelAndView thematicday()
  {
     Map<String, List> map = new HashMap<String, List>(); 
     try{
        List<Themes> theme=dataServices.getTheme();
        List<String> themeday=dataServices.getThematicDate();
         map.put("theme", theme);
         map.put("themeday", themeday);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
     return new ModelAndView("thematicday","map",map);
  }

我减去第一个列表

   <c:forEach var="events" items="${map.theme}">
                            <option value="${events.themeid}">${events.themename}</option>

但是我不知道如何从字符串列表中减去值

道英普尔第二榜

    @SuppressWarnings("unchecked")
@Override
public List<String> getDate() {
     List<ThematicDay> Themedate = null;
    session=sessionFactory.openSession();
    tx=session.beginTransaction();
    Query query = session.createQuery( "select a.themedate,a.thematicdayid from tableone a where a.thematicdayid in(SELECT thematicdayid FROM tabetwo GROUP BY thematicdayid HAVING COUNT(*) < 3)");
    List<Object> result = (List<Object>) query.list();
    List<String> strings = new ArrayList<String>();
    for (Object object : result) {
        strings.add(object != null ? object.toString() : null);
    }
    System.out.println("nn *&*&*&* "+strings);
    for(int i=0; i<strings.size(); i++){
        String stringArray = strings.get(i);
        System.out.println("nn *&*&*&* "+stringArray);

    }
    tx.commit();
    session.close();
    return strings;
}

我使用此代码获取该列表

<c:forEach var="Date" items="${map}">
                     <c:if test="${Date.key == 'themeday'}">
                    <div class="bottom-article">
                        <ul class="meta-post">
                            <li><a href="#" class="tl_1"> ${Date.value[0]} </a></li>
                            <li><a href="#" class="tl_2">${Date.value[1]} </a></li>
                        </ul>
                        </div>
                        </c:if>
                        </c:forEach>

但是得到这样的结果

[Ljava.lang.Object;@762b0f
[Ljava.lang.Object;@4f983

下面的方法可以应用于使用字符串和 Bean 对象列表迭代哈希映射

<c:forEach items="${map}" var="events">
 // iterate the key here
    StringValues= ${events.key} 
 // iterate the values of list here
    <c:forEach items="${events.value}" var="item" >
        ${item} 
    </c:forEach>
 </c:forEach>

看这里 :

  • 迭代哈希图的示例

请尝试使用这个。

<c:forEach var="themedays" items="${map.themeday}">
     <c:foreach var="theme" items="${themedays}">
       <b> ${theme} </b>
    </c:foreach>
</c:foreach>

最新更新