• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Tag Files and attributes

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic