我正在使用 Spring Framework,我想使用 JSTL 的 ForEach
循环在 JSP 中打印一个 HashMap
。
这是我的 Controller 代码:
@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>
还是一样的错误
最佳答案
我觉得是jar冲突
删除这个:
jstl-1.2.jar
来自 WEB-INF/lib
目录
关于java - 在 JSP 中打印 HashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22193382/