Group类中属性private String name
User 类中的属性
private String username
private int age
private Group group
且生成了对应的get和set方法
action中的代码:
Group group = new Group();
group.setName("一班");
List userList = new ArrayList();
for(int i=0; i<10; i++) {
User user = new User();
user.setUsername("u" + i);
user.setAge(18 + i);
user.setGroup(group);
userList.add(userList);
}
request.setAttribute("userList", userList);
jsp中的代码:
<table border="1">
<tr>
<td>用户名</td>
<td>年龄</td>
<td>所属组</td>
</tr>
<c:choose>
<c:when test="${empty userList}">
<tr><td colspan="3" align="center" bgcolor="red">没有找到数据</td></tr>
</c:when>
<c:otherwise>
<c:forEach items="${userList }" var="a">
<tr>
<td>
${a.username } </td>
<td>
${a.age }
</td>
<td>
${a.group.name }
</td>
</tr>
</c:forEach>
</c:otherwise>
</c:choose>
</table>
精慕HU
呼唤远方
相关分类