标题翻译
How to change Map value dynamically in JSTL
问题
以下是您要翻译的内容:
"我们的一个应用程序是使用Spring MVC + JSP构建的。请参考下面的JSP。
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.HashMap"%>
<%
ArrayList<HashMap<String, String>> listOfMap = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for (int i = 0; i < 3; i++) {
map = new HashMap<String, String>();
map.put("key1", "value1" + i);
map.put("key2", "value2" + i);
listOfMap.add(map);
}
request.setAttribute("listOfMap", listOfMap);
%>
<html>
<body>
<c:forEach items="${listOfMap}" var="maps">
<c:forEach items="${maps}" var="mapItem">
${mapItem.key} ${mapItem.value} <br />
</c:forEach>
</c:forEach>
</body>
</html>
正如您在这个JSP中所看到的,我试图迭代Map并在页面上显示键和值。
但是,如果我想在循环内部迭代时更改mapItem.key
和mapItem.value
的值,该怎么办?
所以它将如下所示。
<c:forEach items="${listOfMap}" var="maps">
<c:forEach items="${maps}" var="mapItem">
<!--基本上我将在这里编写代码(在脚本中)以防止跨站点脚本攻击-->
${mapItem.key} ${mapItem.value} <br />
</c:forEach>
</c:forEach>
"
请注意,上面的JSP代码是关于在JSP页面中迭代Map并显示其键和值。在第二个示例中,您提到要在循环内部更改mapItem.key
和mapItem.value
的值以防止跨站点脚本攻击,但是您并没有提供具体的代码。如果您需要更多关于如何实现这一点的帮助,请提供更多上下文或代码示例,我将很乐意协助您。
英文翻译
One of our application built using Spring MVC + JSP.
Please refer below JSP.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.HashMap"%>
<%
ArrayList<HashMap<String, String>> listOfMap = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for (int i = 0; i < 3; i++) {
map = new HashMap<String, String>();
map.put("key1", "value1" + i);
map.put("key2", "value2" + i);
listOfMap.add(map);
}
request.setAttribute("listOfMap", listOfMap);
%>
<html>
<body>
<c:forEach items="${listOfMap}" var="maps">
<c:forEach items="${maps}" var="mapItem">
${mapItem.key} ${mapItem.value} <br />
</c:forEach>
</c:forEach>
</body>
</html>
As you can see in this jsp that i am trying to iterate map and display key and value on page.
But what should be done in case if i want to change the value of mapItem.key
and mapItem.value
while iterating inside the loop.
So it would like below.
<c:forEach items="${listOfMap}" var="maps">
<c:forEach items="${maps}" var="mapItem">
<!--Basically i would write a code (in scriplet) to prevent cross site scripting here -->
${mapItem.key} ${mapItem.value} <br />
</c:forEach>
</c:forEach>
专注分享java语言的经验与见解,让所有开发者获益!
评论