response.setContentType("text/html;charset=UTF-8");
ArrayList<HashMap<String,String>> list=new ArrayList<>();
Enumeration<?> paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
String paramValue =request.getParameter(paramName);
HashMap<String,String> map=new HashMap<>();
map.put(paramName, paramValue);
list.add(map);
}
request.setAttribute("haha", list);
RequestDispatcher rd=request.getRequestDispatcher("qqjstl.jsp");
rd.forward(request, response);;
<c:forEach var="current" items="${haha}" >
<tr>
<td><c:out value="${current.key}"></c:out></td>
<td><c:out value="${current}"></c:out></td>
</tr>
</c:forEach>
請問一下要印出map的key和value ${}要怎麼寫?
感謝><
覺得在 ArrayList 內放 HashMap 怪怪的,
但如果只用 HashMap,forEach 出來的順序會和 put 進去的順序不同,
所以改成 ArrayList 內放字串陣列,
不知道這個是不是您要的
response.setContentType("text/html;charset=UTF-8");
ArrayList<String[]> list = new ArrayList<>();
Enumeration<?> paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
String paramValue =request.getParameter(paramName);
list.add(new String[] { paramName, paramValue });
}
request.setAttribute("haha", list);
RequestDispatcher rd=request.getRequestDispatcher("qqjstl.jsp");
rd.forward(request, response);;
<c:forEach var="current" items="${haha}" >
<tr>
<td><c:out value="${current[0]}"></c:out></td>
<td><c:out value="${current[1]}"></c:out></td>
</tr>
</c:forEach>