【问题标题】:Iterate through nested hash map in jsp [duplicate]遍历jsp中的嵌套哈希映射[重复]
【发布时间】:2014-09-17 19:42:22
【问题描述】:

我已经写了一个这样的嵌套哈希图......

    HashMap<String, String> choice_map = new HashMap<String, String> ();

    choice_map.put("4","<?php ?>");
    choice_map.put("2","<? ?>");
    choice_map.put("1","<? >");
    choice_map.put("3","< ?>");

    HashMap<String, String> que_id_map = new HashMap<String, String> ();

    que_id_map.put("1",null);

    HashMap<String, String> que_type_map = new HashMap<String, String> ();

    que_type_map.put("Objective",null);

    HashMap<String, String> que_map = new HashMap<String, String> ();

    que_map.put("What is Short Tag in PHP?",null);

    HashMap<String, HashMap<String, String>> main_choice_map = new HashMap<String, HashMap<String, String>>();


    main_choice_map.put("question",que_map);
    main_choice_map.put("question_type_id",que_id_map);
    main_choice_map.put("question_type",que_type_map);
    main_choice_map.put("choices",choice_map);

   HashMap<String, HashMap<String, HashMap<String, String>>> questions_with_choices = new HashMap<String, HashMap<String, HashMap<String, String>>> ();

   questions_with_choices.put("2",main_choice_map);

上面的hashmap(questions_with_choices)的输出是这样的:

{2={question_type_id={1=null}, question_type={Objective=null}, 选择={3=>, 2= ?>, 1= >, 4=}, question={什么是 PHP 中的短标签?=null}}}

如何在jsp或jstl中遍历这个hashmap?

请帮帮我..

【问题讨论】:

  • 你知道如何在 Java 中遍历 hashmap 吗?这在 JSP 中是类似的——你只需用 scriptlet 标签包围代码。
  • 谢谢路易吉;我很清楚 JSP 很烂,但 OP 要求在 JSP 或 JSTL 中提供解决方案。

标签: java jsp hashmap nested jstl


【解决方案1】:
<%
Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry pairs = (Map.Entry)it.next();
%>
    <h1><%=pairs.getKey()%> = <%=pairs.getValue()%></h1>
<%
    it.remove(); // avoids a ConcurrentModificationException
}
%>

【讨论】:

  • 感谢您的回答..请提供我的哈希图的完整代码...我会接受您的回答
猜你喜欢
  • 2018-04-08
  • 2019-02-26
  • 1970-01-01
  • 1970-01-01
  • 2012-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多