嘿,大家好,我有一个返回的 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/