How does a scriptlet pass an array to a JSTL tag?

I thought I <%= %>

should be evaluating a string when used in a JSTL 1 context . But it doesn't look like the following code:

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

      

To my surprise, the tag <c:forEach>

actually iterates over the array inside the script:

Item: 1
Item: 2
Item: 3

      

Can someone explain this behavior?

Thank!

Links

0


source to share


1 answer


Answering my question after some reading.

Long story short, I was wrong about how JSP tag attributes are evaluated. If the scriptlet is used to set the value of an attribute to 1, its return value rather than being converted to a string is used directly to set the value of the attribute. (If the types do not match, EL does type coercion to try to make it work. If that fails, an exception is thrown.)

In the example



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

      

the tag attribute type items

Object

, so the attribute is set to the result of the script set - an array new Object[] { 1, 2, 3 }

.

  • Note that you cannot use a scriptlet in combination with a literal string to set an attribute. That is, you might think that the <c:forEach items="abc<%= "def" %>" var="c">

    scriptlet will execute and evaluate the string abcdef

    . But instead, it will set the attribute value items

    for the string only abc<%= "def" %>

    .
0


source







All Articles