Java JSTL forEach在自定义标记中不工作
我的班级:Java JSTL forEach在自定义标记中不工作,java,jsp,jakarta-ee,jstl,el,Java,Jsp,Jakarta Ee,Jstl,El,我的班级: public class CustomObject { private String name; public String getName() { return "test"; } } List<CustomObject> items = new ArrayList<CustomObject>(); items.add(new CustomObject()); request.setAttribute( "item
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 );
<div class="container">
<c:forEach items="${items}" var="item">
${item.name}
</c:forEach>
请求:
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 );
<div class="container">
<c:forEach items="${items}" var="item">
${item.name}
</c:forEach>
List items=new ArrayList();
添加(新的CustomObject());
request.setAttribute(“items”,items);
此示例在我的jsp中运行:
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 );
<div class="container">
<c:forEach items="${items}" var="item">
${item.name}
</c:forEach>
${item.name}
但是当我使用自定义标记时,返回的是错误。为什么不读取属性?
<%@ 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>
控制台:
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 );
<div class="container">
<c:forEach items="${items}" var="item">
${item.name}
</c:forEach>
原因:javax.el.PropertyNotFoundException:类
“java.lang.String”没有属性“name”
指定属性类型,如果未指定属性类型,则
列表
属性类型默认为字符串
:
您能更具体一点吗?如果您关心类型安全,我不确定您为什么要使用jstlI,我只是想知道是否可以指定属性中使用的类,这样我就不必记得在重命名时在jsp中对其进行了更改,但后来我意识到您只指定了属于java sdk的集合类型,在这种情况下,您将永远不会重命名它(dohhhh)。忘了我说的吧。