iT邦幫忙

0

JSTL 如何印出map key&value

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 ${}要怎麼寫?
感謝><

1 個回答

0
fysh711426
iT邦新手 3 級 ‧ 2017-11-18 11:31:46
最佳解答

覺得在 ArrayList 內放 HashMap 怪怪的,
但如果只用 HashMap,forEach 出來的順序會和 put 進去的順序不同,
所以改成 ArrayList 內放字串陣列,
不知道這個是不是您要的
/images/emoticon/emoticon35.gif

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>

可以了 感謝/images/emoticon/emoticon37.gif

可以了 感謝/images/emoticon/emoticon37.gif

/images/emoticon/emoticon42.gif

我要發表回答

立即登入回答