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