java - 在 JSTL 中循环 bean 数组

标签 java jsp jstl

嘿,大家好,我有一个返回的 bean,该 bean 中的一个属性是另一个 bean 的数组。

我可以在循环时很好地获取其他属性并显示在 jsp 上,但我需要获取数组的内容。我知道我做错了并且忘记了类似的语法是什么。

实际上我需要最后一列中的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 :

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; } 

Role 是另一个 bean,它的 getter 是 getName()getId()

我需要在最后一列中显示 id 数组,但我只是获取一个内存位置...

最佳答案

你还需要循环你的角色

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

另一种可能性是

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

如果您只是在寻找第一个角色。

关于java - 在 JSTL 中循环 bean 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9737388/

相关文章:

java - 通过 h2 浏览器和控制台与 PlayFramework 中的 EBean 模型类进行交互

java - jSTL:无法解析表达式 [${item.class.name == 'List' }]

java - 我在哪里可以下载 JSTL 标签库?即 jSTL.jar 和 standard.jar

java - 为什么 Java 使用 int i = 1<<4,而不是 int i = 16?

java - 将数组分成两半并找到两个最大值,然后合并这两个值

没有模式的 Java REST 客户端

java - 如何将对象从 Spring 3.0 Controller 传递到 JSP View 并使用 JSTL 进行测试

javascript - 需要使用JS获取<a>标签的 "id"属性

java - 为什么我的 jspSmartUpload 代码不起作用?

java - 可以将 Java Swing 包含在 JSP 中吗?