Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a generic List to a JSP tag

Tags:

java

jsp

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.

like image 872
Portman Avatar asked Mar 26 '12 16:03

Portman


2 Answers

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

like image 66
BalusC Avatar answered Oct 21 '22 10:10

BalusC


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

like image 43
5AR Avatar answered Oct 21 '22 08:10

5AR