先引用标签库
<%@ taglib prefix="s" uri="/struts-tags"%>
下面简单例子:
1
首先有一个User对象:
public class User { private Integer userId; private String userName; private String userPwd; //get和set方法 ........... }
action中的代码:
在把list放在request中,好在jsp页面中能拿到
HttpServletRequest req = getRequest();
req.setAttribute("list", list);
最后在jsp页面中的代码
<table> <s:iterator value="#request.list" id="user">// <tr> <td<s:property value="user.userId"/></td>//这也可以写成${user.userId}/> <td><s:property value="user.userName"/></td> <td><s:property value="user.userPwd"/></td> </tr> </s:iterator> </table>sd
其实就类似于java里的foreach循环for(Account account :list){sysout(account.getName());//简写输出语句}其实就类似于java里的foreach循环for(Account account :list){sysout(account.getName());//简写输出语句}其实就类似于java里的foreach循环for(Account account :list){sysout(account.getName());//简写输出语句}其实就类似于java里的foreach循环
for(User user :#request.list){
sysout(user.getUserId());//简写输出语句
}
<table> <s:iterator value="list" id="别名">//这个id加不加都没事 <tr> <td><s:property value="user.userId"/></td>//这也可以写成<s:property value="别名.user.userId"/> <td><s:property value="user.userName"/></td> <td><s:property value="user.userPwd"/></td> </tr> </s:iterator> </table>