Portman
Portman

Reputation: 31975

Pass a generic List to a JSP tag

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:

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.

Upvotes: 16

Views: 15530

Answers (2)

5AR
5AR

Reputation: 11

I had same problem, but I realized that I was sending String not actual Object. Maybe you had same mistake. :)

Upvotes: 1

BalusC
BalusC

Reputation: 1108712

You don't need to specify the generic type. The type="java.util.List" must work. Your concrete problem is caused elsewhere.

Upvotes: 20

Related Questions