I'm using JSP tags to encapsulate reusable front-end logic.
I can successfully pass a complex object com.example.Product
to a tag, but I'm having trouble passing a List<Product>
to a tag.
Here is my product.tag
:
<%@ attribute name="product" required="true" type="com.example.Product" %>
<a href="/products/${product.id}/${product.slug}">${product.name}</a>
I can use this on a JSP page like so:
<%@ taglib tagdir="/WEB-INF/tags" prefix="h" %>
<h:product product="${myProduct}"/>
Now, I would like to create a tag to display a list of products. I'm stuck on how to describe the type in the attribute declaration:
<%@ attribute name="products" required="true" type="???" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<ul>
<c:forEach items="${products}" var="product">
<li><h:product product="${product}"/></li>
</c:forEach>
</ul>
I've tried the following:
List<com.example.Product>
java.util.List<com.example.Product>
Both yield the following error: Unknown attribute type (java.util.List<com.example.Product>) for attribute products
I'm sure there's just some syntax for how to describe a generic type in the attribute directive, but I can't find any examples.
You don't need to specify the generic type. The type="java.util.List"
must work. Your concrete problem is caused elsewhere.
I had same problem, but I realized that I was sending String not actual Object. Maybe you had same mistake. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With