2

I found difficulties in iterating a multilevel collection on 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 code

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

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

tag file:

<%@ 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>

if i use 'list' in <c:forEach var="entity" items="${list}"> like here I cannot use ${entity.name}, if use <c:forEach var="entity" items="${establishmentList}"> i get recursive call of first-only element. So, how should I edit tag file to make this work?

1 Answer 1

0

The solution isn't so far :)

The line <%@ attribute name="list" required="true" %> is responsible for the configuration of the types we want to iterate. If we want to operate with ArrayList (this case) we should use <%@ attribute name="list" type="java.util.ArrayList<by.ipps.accounting.model.Establishment>" required="false" %>.

I've found the idea of "type" parameter here but the things are seem not to be the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.