无法使用jstl迭代hashmap的arraylist

uyhoqukh  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(394)

我无法使用jstl打印或从hashmap的arraylist获取数据。我想在表格的arraylist中显示每个bug的详细信息,比如bug的id、创建日期、摘要、白板。下面是jsp和servlet代码。请建议修理。
家。jsp:-

<form action="IncomingBugs">
<table class="ui single line table">
<thead>
<tr>
  <th>Bug Id</th>
  <th>Creation Date</th>
  <th>Summary</th>
  <th>Whiteboard</th>
</tr>
</thead>
<tbody>
  <c:forEach var="row" items="${list}"> 
    <tr>
    <td><c:out value="${row.bug_id}"/></td>
    <td><c:out value="${row.creation_ts}"/></td>
    <td><c:out value="${row.short_desc}"/></td>
    <td><c:out value="${row.status_whiteboard}"/></td>
</tr>
 </c:forEach> 
 </tbody>
 </table>
 </form>

虫子。jsp:-

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
            OracleConnection oc=new OracleConnection();

    try {
        ArrayList<HashMap<String, Object>> bugList=oc.getbugList(null);

        ArrayList<HashMap<String, String>> blr=new ArrayList<HashMap<String, String>>();
         for (HashMap<String, Object> bugMap : bugList) 
        {
             HashMap<String,String> bl=new HashMap<String, String>();
             String bugzilla_id = bugMap.get("bug_id").toString();
             String creation_ts = bugMap.get("creation_ts").toString();
             String short_desc = bugMap.get("short_desc").toString();
             String status_whiteboard=bugMap.get("status_whiteboard").toString();
             bl.put("bug_id", bugzilla_id);
             bl.put("creation_ts", creation_ts);
             bl.put("short_desc", short_desc);
             bl.put("status_whiteboard", status_whiteboard);
             blr.add(bl);
        }
         request.setAttribute("list",blr);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/home.jsp");
        dispatcher.forward(request, response);

    }

下面是我想要打印的java示例代码table:-

ArrayList<HashMap<String, Object>> bugList = (ArrayList<HashMap<String, Object>>)request.getAttribute("list");
 for (HashMap<String, Object> bugMap : bugList) 
{           
     String bugzilla_id = bugMap.get("bug_id").toString();
     String creation_ts = bugMap.get("creation_ts").toString();
     String summary = bugMap.get("summary").toString();
     String status_whiteboard=bugMap.get("status_whiteboard").toString();
     out.println("<tr>");
     out.println("<td>"+bugzilla_id+"</td>");
     out.println("<td>"+creation_ts+"</td>");
     out.println("<td>"+summary+"</td>");
     out.println("<td>"+status_whiteboard+"</td>");
     out.println("</tr>");
}
7vhp5slm

7vhp5slm1#

通过将incomingbugs.java的代码移到jsp中解决了这个问题,但是我仍然无法理解为什么arraylist没有从java传递到jsp文件?我们不能这样从jsp'form action=“incomingbugs”'引用servlet吗?
下面是fix:-

<%
            OracleConnection oc=new OracleConnection();     
            try {
                ArrayList<HashMap<String, Object>> bugList=oc.getbugList(null);

                ArrayList<HashMap<String, String>> blr=new ArrayList<HashMap<String, String>>();
                 for (HashMap<String, Object> bugMap : bugList) 
                {
                     HashMap<String,String> bl=new HashMap<String, String>();
                     String bugzilla_id = bugMap.get("bug_id").toString();
                     String creation_ts = bugMap.get("creation_ts").toString();
                     String short_desc = bugMap.get("short_desc").toString();
                     String status_whiteboard=bugMap.get("status_whiteboard").toString();
                     bl.put("bug_id", bugzilla_id);
                     bl.put("creation_ts", creation_ts);
                     bl.put("short_desc", short_desc);
                     bl.put("status_whiteboard", status_whiteboard);
                     blr.add(bl);

                }
                 request.setAttribute("list",blr);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
          %>

  <c:forEach items="${list}" var="maps">
  <tr>
  <td>${maps['bug_id']} </td>
  <td>${maps['creation_ts']} </td>
  <td>${maps['short_desc']} </td>
  <td>${maps['status_whiteboard']} </td>
  </tr>
  </c:forEach>
b4qexyjb

b4qexyjb2#

像这样试试

<td><c:out value="${row.get('bug_id')}"/></td>
<td><c:out value="${row.get('creation_ts')}"/></td>
<td><c:out value="${row.get('summary')}"/></td>

你有Map列表。所以本质上,行在每次迭代中保存一个Map。你可以用这个符号 row.get('key_name') 从Map上获取该键的值。
还要确保这个taglib作为第二行添加到jsp页面的顶部。

<%@ taglib prefix="c"       uri="http://java.sun.com/jsp/jstl/core" %>

相关问题