java 将通用列表传递给 JSP 标记
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9876366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass a generic List to a JSP tag
提问by Portman
I'm using JSP tags to encapsulate reusable front-end logic.
我正在使用 JSP 标记来封装可重用的前端逻辑。
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.
我可以成功地将一个复杂的对象传递com.example.Product
给一个标签,但我无法将一个传递List<Product>
给一个标签。
Here is my product.tag
:
这是我的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:
我可以像这样在 JSP 页面上使用它:
<%@ 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>
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
两者都会产生以下错误: 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.
我确信在属性指令中只有一些语法可以描述如何描述泛型类型,但我找不到任何示例。
回答by BalusC
You don't need to specify the generic type. The type="java.util.List"
must work. Your concrete problem is caused elsewhere.
您不需要指定泛型类型。的type="java.util.List"
必须工作。你的具体问题是在别处引起的。
回答by 5AR
I had same problem, but I realized that I was sending String not actual Object. Maybe you had same mistake. :)
我遇到了同样的问题,但我意识到我发送的是 String 而不是实际的 Object。也许你也犯过同样的错误。:)