【问题标题】:How to iterate Map<String, List> map = new HashMap<String, List>() in a jsp page using JSTL如何使用 JSTL 在 jsp 页面中迭代 Map<String, List> map = new HashMap<String, List>()
【发布时间】:2015-01-21 05:45:48
【问题描述】:

我将两个列表添加到一个 Map map = new 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>

但我不知道如何从字符串列表中提取值

daoImpl 中的第二个列表

    @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("\n\n *&*&*&* "+strings);
    for(int i=0; i<strings.size(); i++){
        String stringArray = strings.get(i);
        System.out.println("\n\n *&*&*&* "+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

【问题讨论】:

  • 你需要观察你得到的List&lt;Object&gt;,然后填充List&lt;String&gt;,不要使用object.toString(),迭代List&lt;Object&gt;并获取themedate的值并填写@987654331 @

标签: hibernate jsp spring-mvc hashmap jstl


【解决方案1】:

以下方法可用于使用字符串和 bean 对象列表迭代 hashmap

<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>

请看这里:

【讨论】:

  • 这里也打印列表的值 [Ljava.lang.Object;@1a99a72 [Ljava.lang.Object;@1dada3.实际上我需要得到结果 01/14/2015 DAY100 02/14/2015 DAY107
  • @Priyanka 似乎您没有正确访问 bean 属性,您可以使用您现在尝试的代码更新您的问题吗?
  • 但我不能使用 List result = (List) query.list() 而不是 List result = (List) query.list() .您能否解释一下如何将值存储在我的 DaoImpl 中的列表中。
【解决方案2】:

请试试这个。

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

【讨论】:

  • 对不起,我得到了“themedays themedays”的结果。实际上我需要得到结果 01/14/2015 DAY1007 02/14/2015 DAY1008
  • 获取值 [Ljava.lang.Object;@a3e5fc [Ljava.lang.Object;@502bb9.不是确切的值。
  • @PriyankaShaju 刚刚观察到,你在做object.toString(),你得到了什么对象,你调试了吗,你看到你的列表了吗。您正在将object.toString() 添加到您的List&lt;String&gt;,问题本身就在这里,请更正。您可能需要在结果上调用 getter 或 result.get("themedate") 之类的东西,然后添加到 List&lt;String&gt;
  • 但我不能使用 List result = (List) query.list();而不是 List 结果 = (List) query.list();。当我使用这个 List result = (List) query.list(); for(ThematicDay p:result) { System.out.println("*************************"+p.getThemedate());它显示错误" ​​java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.astra.model.ThematicDay "
猜你喜欢
  • 2016-01-12
  • 1970-01-01
  • 2018-06-19
  • 2014-10-19
  • 1970-01-01
  • 1970-01-01
  • 2011-04-07
  • 2018-09-22
  • 1970-01-01
相关资源
最近更新 更多