scriptlet如何将数组传递给JSTL标签? [英] How does a scriptlet pass an array to a JSTL tag?

查看:85
本文介绍了scriptlet如何将数组传递给JSTL标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为<%= %>在JSTL 1 的上下文中使用时应该计算为字符串.但这在下面的代码中似乎并非如此:

I thought <%= %> is supposed to evaluate to a string when used in the context of JSTL1. But this does not seem to be the case in the code below:

<c:forEach var="item" items="<%= new Object[] { 1, 2, 3 } %>">
Item: ${item}
</c:forEach>

令我惊讶的是,<c:forEach>标记实际上遍历了scriptlet内部的数组:

To my surprise, the <c:forEach> tag actually iterates over the array inside the scriptlet:

Item: 1
Item: 2
Item: 3

有人可以解释这种行为吗?

Can someone please explain this behavior?

谢谢!

  1. JSTL< c:if>中的测试属性;标签

推荐答案

阅读后回答我自己的问题.

Answering my own question after some reading.

简而言之,我对如何评估JSP标记属性是错误的.如果使用scriptlet设置属性 1 的值,则其返回值(而不是转换为字符串)将直接用于设置属性的值. (如果类型不匹配,则EL会强制执行类型强制以使其起作用.如果失败,则会引发异常.)

In short, I was wrong about how JSP tag attributes are evaluated. If a scriptlet is used to set the value of an attribute1, its return value, rather than being converted to a string, is used directly to set the value of the attribute. (If the types don't match, EL performs type coercion to try and make it work. If that fails, an exception is raised.)

在示例中

<c:forEach var="item" items="<%= new Object[] { 1, 2, 3 } %>">

标记的items属性的类型为Object,因此该属性设置为scriptlet—数组new Object[] { 1, 2, 3 }的结果.

the type of the tag's items attribute is Object, so the attribute is set to the result of the scriptlet—the array new Object[] { 1, 2, 3 }.

  1. 请注意,您不能将scriptlet与文字字符串结合使用来设置属性.也就是说,您可能认为<c:forEach items="abc<%= "def" %>" var="c">将执行脚本并评估为字符串abcdef.而是将items的属性值设置为仅字符串abc<%= "def" %>.
  1. Note that you can't use a scriptlet in combination with literal string to set an attribute. That is, you might think <c:forEach items="abc<%= "def" %>" var="c"> will execute the scriptlet and evaluate to the string abcdef. But instead, it will set the attribute value of items to just the string abc<%= "def" %>.

这篇关于scriptlet如何将数组传递给JSTL标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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