例:留言板中在userlist.jsp页面循环遍历user时,同时把user所对应的留言显示出来
一、在model中
public class User{
private Integer userId;
private String username;
private String password;
private List listNote ;
……
getter and setter
……
}
public class Note {
private Integer noteId;
private String date;
private String content;
private Integer userId;
private List list;
……
getter and setter
……
}
二、在Action中
public class UserAction{
private User user;
private UserService userService;
private NoteService noteService;
private List list ;
……
getter and setter
……
public String execute ()throws Exception{
return listUser();
}
public String listUser(){
list =userService.findAll();
Iterator it=list .iterator();
while(it.hasNext()){
User temp=(User)it.next();
List listNote=noteService.findByUserId(temp.getUserId());
temp.setListNote (listNote);
}
}
}
三、userlist.jsp页面中
<s:iterator value="#request.list " id="user ">
<tr>
<td><s:property value="userId"/></td>
<td><s:property value="username"/></td>
<td><s:property value="password"/></td>
</tr>
<s:iterator value="#user .listNote" id="note">
<tr>
<td><s:property value="content"/></td>
</tr>
</s:iterator>
</s:iterator>