每个JSTL,var包含方括号 [英] JSTL for each, var contains square brackets

查看:86
本文介绍了每个JSTL,var包含方括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串HashSet,它是由以下代码制成的:

I have a HashSet of Strings, which is made with the following code:

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);

现在在带有JSTL的JSP页面中,我执行了一个普通的forEach循环:

Now in a JSP page, with JSTL, I do a normal forEach loop:

<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> 

请注意出现在第一个脚本源之前和之后的方括号([]).它们来自哪里?

Notice the square brackets ([ and ]) that appear before the first script source and after the last. Where do they come from?

推荐答案

之所以出现此问题,是因为scripts变量是通过自定义标记的属性在JSP中设置的,如下所示:

The problem occured because the scripts variable was set in a JSP through an attribute for a custom tag, like this:

<t:genericpage scripts="${scripts}">
....

当然,这可以通过调用其toString()方法将集合转换为字符串.通过在Servlet中设置request属性,我们以另一种方式解决了该问题.

Of course, this converted the collection to a string by calling its toString() method. We have solved it in a different way, by setting the request attribute in the servlet.

这篇关于每个JSTL,var包含方括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆