jsp中迭代列表时的问题
我的 jsp 文件中有以下代码:
<c:set var="urls" value="<%LoginServlet.getAvailableUrlsConfig();%>" />
<c:forEach var="url" items="${urls}">
<c:out value="${url}"/>
</c:forEach>
getAvailableUrlsConfig()
是一个返回字符串数组的静态方法。
我不明白为什么它不打印任何内容。..
如果我使用它:
<%out.println(LoginServlet.getAvailableUrlsConfig()); %>
它会打印出列表的内容。你能看到任何问题吗?
I have the following code in my jsp file:
<c:set var="urls" value="<%LoginServlet.getAvailableUrlsConfig();%>" />
<c:forEach var="url" items="${urls}">
<c:out value="${url}"/>
</c:forEach>
getAvailableUrlsConfig()
is a static method returning an array of Strings.
I don't understand why it does not prints out anything...
If I use this:
<%out.println(LoginServlet.getAvailableUrlsConfig()); %>
it prints out the content of the list. Can you see any issues?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这对我有用:
似乎它不喜欢使用 <% %>在jstl中
This worked for me:
Seems that it does not like to use <% %> in jstl
@Cristian,我不会称自己为专家,但我对你的问题的一些想法,ps。尝试将此添加为评论,但我需要更多字符!
我相信在您的
c:set
中,您需要将语法作为表达式(而不是 scriplet,这不会打印任何内容,因为它被<%..%>< 包围) /code> 标签。)。
表达式
<%=..%>
= 使用表达式,计算表达式的结果将转换为字符串并直接包含在输出页面中。这里的=
部分表示它应该打印出标签内代码的返回值,而 scriptlet 是语句。请注意,当您使用表达式时,它不需要分号。恕我直言,我认为如果可能的话最好避免 scriplet/表达式,我会在请求/会话上设置任何值或在页面 bean 对象上设置它们。这样,jsp 就干净并且有利于可读性,但当然取决于您的特定问题/场景。
您可能会发现以下链接也很有用 http://java.sun.com/ developer/onlineTraining/JSPIntro/contents.html & http://java.sun.com/j2ee/tutorial/1_3 -fcs/doc/JSPIntro7.html
希望这有帮助。
@Cristian I will not call myself an expert but some of my thoughts on your issue, ps. tried to add this as a comment but I need more chars!
I believe in your
c:set
you would need to have the syntax as an expression (than scriplet, this will not print anything since it's surrounded with a<%..%>
tag.).Expressions
<%=..%>
= With expressions the results of evaluating the expression are converted to a string and directly included within the output page. Here the=
part indicates that it should print out the return value of the code inside the tag, whereas the scriptlets are statements. Note that when your using an expressions it does not require a semicolon.IMHO I think if possible it is better to avoid scriplets/expression where possible, I would set any values on request/session or set them on page bean object. This way the jsps are clean and favours readability but of course depends on your particular issue/scenario.
You might find the following link useful as well http://java.sun.com/developer/onlineTraining/JSPIntro/contents.html & http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPIntro7.html
Hope this helps.