我的类(class):
public class CustomObject {
private String name;
public String getName() {
return "test";
}
}
请求:
List<CustomObject> items = new ArrayList<CustomObject>();
items.add(new CustomObject());
request.setAttribute( "items", items );
这个例子在我的 jsp 中运行:
<div class="container">
<c:forEach items="${items}" var="item">
${item.name}
</c:forEach>
但是当我使用自定义标签时返回错误。为什么不读属性?
<%@ attribute name="list" required="true" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:if test="${!empty list}">
<c:forEach var="item" items="${list}">
<li><c:out value="${item.name}"/></li>
</c:forEach>
</c:if>
控制台:
Caused by: javax.el.PropertyNotFoundException: The class 'java.lang.String' does not have the property 'name'.
最佳答案
指定属性类型,不指定list
属性类型默认为 String
:
<%@ attribute name="list" required="true" type="java.util.List" %>
关于java - JSTL forEach 在自定义标签中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25150537/