簡體   English   中英

在JSTL中循環遍歷bean數組

[英]Loop over bean array In JSTL

大家好,我有一個bean返回,並且bean中的屬性之一是另一個bean的數組。

我可以很好地獲取其他屬性,並在循環時顯示在jsp上,但是我需要獲取數組的內容。 我知道即時通訊做錯了,忘記了類似的語法。

Baaically我需要role.id在最后一欄。 這就是我得到的:

JSP:

   <table class="data_table">
    <c:choose>
        <c:when test='${empty attachList}'>
           <tr>
              <td>No Attachments</td>
           </tr>
        </c:when>
        <c:otherwise>
            <tr>
                <th>Remove Attachment</th>
                <th>File Name</th>
                <th>File Type</th>
                <th>File Size (bytes)</th>
                <th>File Attached By</th>
                <th>Date/Time File Attached</th>
                <th>Roles</th>
            </tr>
            <c:forEach var="item" items="${attachList}" varStatus="loopCount">
                <tr>

                    <td class="button">
                    <rbac:check operation="<%=Operation.DELETE%>">
                        <button type="button"  onclick="javascript:delete_prompt(${item.id});">Delete</button>
                    </rbac:check>
                        </td>
                    <td><a href="show.view_hotpart_attachment?id=${item.id}">${item.fileName}</a></td>
                    <td>${item.fileType}</td>
                    <td><fmt:formatNumber value="${item.fileSize}" /></td>
                    <td>${item.auditable.createdBy.lastName}, ${item.auditable.createdBy.firstName}</td>
                    <td><fmt:formatDate value="${item.auditable.createdDate}" pattern="${date_time_pattern}" /></td>
                    <td>${item.roles}</td>

                </tr>
            </c:forEach>
        </c:otherwise>
    </c:choose>
    </table>

豆角,扁豆:

Bean類在items="${attachList}"循環

private long id;
private String fileName;
private String fileType;
private int fileSize;
private Role[] roles;
private AuditableBean auditable;

/**
 * Constructor.
 */
public AttachmentShortBean()
{
}

public long getId() { return id; }

public String getFileName() { return fileName; }

public String getFileType() { return fileType; }

public int getFileSize() { return fileSize; }

public Role[] getRoles() { return roles; }

public AuditableBean getAuditable() { return auditable; } 

角色是另一個bean,其getName()getName()getId()

我需要在最后一列中分配ID數組,但是我只是獲得一個內存位置...

您還需要遍歷您的角色

<c:forEach items = "${item.roles}" var = "role">
   <c:out value = "${role.id}"/>
</c:forEach>

另一種可能性是

<c:out value = "${item.roles[0].id}"/>

如果您只是在尋找第一個角色。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM