java在JSP中打印HashMap
我使用的是Spring框架,我想使用JSTL的ForEach
循环在JSP中打印HashMap
这是我的控制器代码:
@RequestMapping(value = "/addContact", method = RequestMethod.POST)
public ModelAndView addContact(@ModelAttribute("contact")
Contact contact, BindingResult result)
{
ModelMap modelMap = new ModelMap();
String msg = "Jon Doe added to database";
modelMap.put("message", msg);
Map<String, String> myMap = new HashMap<String, String>();
myMap.put("A", "DD");
myMap.put("B", "EE");
myMap.put("C", "FF");
System.out.println("***********************************");
for (Map.Entry<String, String> entry : myMap.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
modelMap.put("myMap", myMap);
return new ModelAndView("hello", modelMap);
}
下面是我的JSP:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hi there</title>
</head>
<body>
<h2>Hello</h2>
<c:out value="${message}"></c:out> <br> <br> <br>
<a href="http://localhost:8080/SpringMVC/">back</a>
<br>
<c:forEach items="${myMap}" var="entry">
Key : <c:out value="${entry.key}"/> Value: <c:out value="${entry.value}"/> <br />
</c:forEach>
</body>
</html>
我使用了in this accepted answer.解释的逻辑
But still I am unable to print the HashMap. Getting error HTTP Status 500
以下是我的控制台输出:
13:23:11,756 INFO [EARDeployer] Started J2EE application: file:/C:/jboss-4.0.2_1/server/default/deploy/SpringMVCEAR.ear
13:23:54,863 INFO [STDOUT] ***********************************
13:23:54,863 INFO [STDOUT] Key = A, Value = DD
13:23:54,863 INFO [STDOUT] Key = B, Value = EE
13:23:54,863 INFO [STDOUT] Key = C, Value = FF
13:23:55,098 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception
java.lang.NoClassDefFoundError: javax/el/ValueExpression
at java.lang.Class.getDeclaredMethods0(Native Method)
When I remove the forEach tag in my JSP my page is getting displayed properly with message Jon Doe added to database
提前感谢您的帮助:)
编辑我也尝试过不使用forEach循环体:
<c:forEach items="${myMap}" var="entry">
Something
</c:forEach>
还是同样的错误
共 (0) 个答案