我无法使用jstl从hashmap的arraylist中打印或获取数据。我想在表格中显示arraylist中存在的每个bug的bug_id,创建日期,摘要,白板等详细信息。下面是jsp和servlet代码。请建议修复。
针对home.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>
IncomingBugs.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示例代码: -
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>");
}
答案 0 :(得分:0)
试试这个
<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>
您有一张地图列表。所以基本上该行在每次迭代中都有一个映射。您可以使用符号row.get('key_name')
从地图中获取该键的值。
并确保将此taglib添加到jsp页面的顶部作为第二行。
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
答案 1 :(得分:0)
通过将IncomingBugs.java的代码移动到jsp来解决它,但我仍然无法弄清楚为什么arraylist没有从java传递到jsp文件? 我们不能从jsp&#39; form action =&#34; IncomingBugs&#34;&#39;中引用servlet。以这种方式?
以下是修复: -
<%
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>