在jsp中迭代多级集合

时间:2015-03-26 07:17:35

标签: java jsp collections multi-level

我发现在jsp上迭代多级集合时遇到了困难。

@Entity
@Table(name = "Establishment")
public class Establishment extends Basis<Long> {

@Column(name = "code")
private Long code;    // will be used for link to the entity if the name will be updated
@Column(nullable = false)
private String name;
private String establishmentType;

@ManyToOne(optional = true)
private Establishment establishment;
@Column(name = "establishment_id", insertable = false, updatable = false)
private Long establishmentId;

@OneToMany(mappedBy = "establishment", orphanRemoval = true, cascade = CascadeType.ALL)
private List<Establishment> establishments = new ArrayList<>();

//getters & setters
}

jsp代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page session="true" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="myTags" %>

<myTags:hierarchy list="${establishmentList}"/>

标记文件:

<%@ attribute name="list" required="true" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="myTags" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:if test="${!empty list}">
<ul>
    <c:forEach var="entity" items="${list}">
        <li><c:out value="${entity.name}"/></li>
        <%--${fn:length(e.establishments)}--%>
        <myTags:hierarchy list="${entity.establishments}"/>
    </c:forEach>
</ul>
</c:if>

如果我在<c:forEach var="entity" items="${list}"> here中使用'list',我就不能使用${entity.name},如果使用<c:forEach var="entity" items="${establishmentList}">,我会获得第一个元素的递归调用。那么,我应该如何编辑标签文件以使其工作?

1 个答案:

答案 0 :(得分:0)

解决方案不是到目前为止:)

<%@ attribute name="list" required="true" %>负责配置我们想要迭代的类型。如果我们想使用ArrayList(本例),我们应该使用<%@ attribute name="list" type="java.util.ArrayList<by.ipps.accounting.model.Establishment>" required="false" %>

我发现了“类型”参数here的想法,但事情似乎并不相同。