This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tag Files and attributes

 
Katrin Perry
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I wonder if anyone knows the answer to this:

I am a bit puzzled about how this scripting expression is evaluated:

<%= (List<Product>)request.getAttribute("products") %>

I have a tag file: product.tag

<%@ attribute name="product" type="com.example.Product"required="true" rtexprvalue="true" %>

<div>
${product.name}

${product.price}
</div>

I use the tag file in Products.jsp (I have previously set request scope attribute "products" which is a List<Product))
this is version one of Products.jsp


><%@ taglib prefix="p" tagdir="/WEB-INF/tags/" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.util.List,com.example.Product" %>

<html>
<body>
<%= (List<Product>)request.getAttribute("products") %>

<c:forEach items="${requestScope.products}" var="product" >
<table border="1">
<tr> <td>
<p:product product="${product}"/>
</td></tr>
</table>
</c:forEach>


</body>
</html>

The above works as expected. prints something like this:

[com.example.Product@1cfd3b2, com.example.Product@1536eec]
Product one
3.50£
Product two
9.99£


But if I change Products.jsp to:

<%@ taglib prefix="p" tagdir="/WEB-INF/tags/" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.util.List,com.example.Product" %>

<html>
<body>


<c:forEach items='<%= (List<Product>)request.getAttribute("products") %>' var="product" >
<table border="1">
<tr> <td>
<p:product product="${product}"/>
</td></tr>
</table>
</c:forEach>


</body>
</html>

I get the output:

Product one
3.50£
Product two
9.99£

What I am puzzled about it how come <%= (List<Product>)request.getAttribute("products") %>
returns correctly a list that becomes the attribute of the tag file. when I was expecting that the result of it would be what
out.print would print - some kind of string?


Thanks
Katrin
 
Dumitru Postoronca
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess just as you defined your tag file to have an attribute

that has rtexprvalue="true", the same was done for the forEach tag so it can receive references to objects by using either scripting or EL expressions.

On the other hand, you can never pass a String as the items attribute definitely expects a List.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic