【发布时间】:2012-12-05 17:50:17
【问题描述】:
我有一个HashSet 的字符串,它是用以下代码制作的:
Set<String> scripts = new HashSet<>();
String contextPath = request.getContextPath();
scripts.add(contextPath + "/resources/scripts/jquery.cycle2.js");
scripts.add(contextPath + "/resources/scripts/jquery.cycle2.center.js");
scripts.add(contextPath + "/resources/scripts/slideshow.js");
request.setAttribute("scripts", scripts);
现在在一个 JSP 页面中,使用 JSTL,我执行一个普通的 forEach 循环:
<c:if test="${not empty scripts}">
<c:forEach var="script" items="${scripts}" >
<script type="text/javascript"
src="${script}">
</script>
</c:forEach>
</c:if>
加载页面时,结果如下:
<script type="text/javascript"
src="[/InfoKiosk/resources/scripts/jquery.cycle2.center.js">
</script>
<script type="text/javascript"
src=" /InfoKiosk/resources/scripts/jquery.cycle2.js">
</script>
<script type="text/javascript"
src=" /InfoKiosk/resources/scripts/slideshow.js]">
</script>
注意出现在第一个脚本源之前和最后一个脚本源之后的方括号([ 和])。他们来自哪里?
【问题讨论】:
-
尝试使用
List<String>而不是Set。 -
@JasperdeVries 我们最初使用了 List,我们尝试了 Set 试图解决问题。原因是完全不同的,我将在自己的回答中解释。