今天来看看Struts2的控制标签iterator,if…else…的魅力。之前用过,但总是不是很明白,随着我对Struts2的值栈(ValueStack)与ActionContext的理解渐渐加深后,发现控制标签其实也是很容易滴~~~
我建了一个JavaBean,代码如下:
package com.iman.model; public class Song { private String title; private String singer; private String album; private String publicTime; public Song(String title, String singer, String album, StringpublicTime) { super(); this.title = title; this.singer = singer; this.album = album; this.publicTime = publicTime; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getSinger() { return singer; } public void setSinger(String singer) { this.singer = singer; } public String getAlbum() { return album; } public void setAlbum(String album) { this.album = album; } public String getPublicTime() { return publicTime; } public void setPublicTime(String publicTime) { this.publicTime = publicTime; } }
建一个action文件ControlAction.java,里面有一个类型为list的属性songs,并给这个list添加数值,代码如下:
package com.iman.action; import java.util.ArrayList; import java.util.List; import com.iman.model.Song; import com.opensymphony.xwork2.ActionSupport; public class ControlAction extends ActionSupport { private List<Song> songs=new ArrayList<Song>(); public StringiteratorSongs() { songs=newArrayList<Song>(); // TODOAuto-generated method stub songs.add(newSong("一天一万年","游鸿明","一天一万年","1998")); songs.add(newSong("五月的雪","游鸿明","五月的雪","1999")); songs.add(newSong("下沙","游鸿明","下沙","2000")); songs.add(newSong("恋上另一个人","游鸿明","恋上游鸿明","2002")); songs.add(newSong("楼下那个女人","游鸿明","台北寂寞部屋","2003")); songs.add(newSong("第一个昼夜","游鸿明","2004秋季恋歌","2004")); songs.add(newSong("白色恋人","游鸿明","诗人的眼泪","2006")); songs.add(newSong("夫妻脸","游鸿明","倦鸟余花","2007")); songs.add(newSong("花沙","游鸿明","与爱情无关","2009")); return SUCCESS; } public List<Song>getSongs() { return songs; } public voidsetSongs(List<Song> songs) { this.songs =songs; } }
现在用<s:iterator />标签将其输出。Iterator标签主要用于将集合迭代输出,这里的集合可以是List,Set,也可以是Map,它有如下几个属性:
Value:这个属性用于放置需要输出的集合,是一个可选属性,如果没有,则默认输出值栈(ValueStatck)栈顶的集合;
Id:用于给集合指定id,是一个可选属性;
Status:给属性指定迭代时的属性,通过该实例判断当前迭代元素的属性,也是一个可选属性。它有如下所示的属性:
1.Int getCount:返回当前迭代了几个元素;
2.Int getIndex:返回当前迭代元素的索引;
3.Boolean isEven():返回当前迭代元素的索引是否是偶数;
4.Boolean isFirst():返回当前被迭代的元素是否是第一个元素;
5.Boolean isLast():返回当前被迭代元素是否是最后一个元素;
6.Boolean isOdd():返回当前被迭代的元素是否是一个奇数;
一般情况下status属性与<s:if />…<s:else />标签一起使用。
先看看怎么用iterator标签将action的list输出,jsp页面如下所示:
<%@ page language="java"contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s"uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type"content="text/html; charset=UTF-8"> <title>控制标签</title> <style type="text/css"> table.demo{border-collapse: collapse;width: 400px;height: 300px;text-align: center;} table.demo th,td {padding: 0; border: 1px solid #000;} </style> </head> <body> <table class="demo"> <tr> <td>歌曲</td> <td>歌手</td> <td>专辑</td> <td>发布时间</td> </tr> <s:iterator value="songs"status="songsStatus"> <tr> <td><s:property value="title"/></td> <td><s:property value="singer"/></td> <td><s:property value="album"/></td> <td><s:property value="publicTime"/></td> </tr> </s:iterator> </table> </body> </html>
如上所示,value指的是action中的list属性,而<s:property />的value则指的是JavaBean类Song中的各属性,名字必须一致。
打开服务器并部署项目,在地址栏输入:http://localhost:8000/Struts2Tutorial/iteratorSongs,页面如下所示:
强大吧?现在来看看将<s:iterator />的status属性与<s:if />…<s:else />标签一起使用的情况。Status创建的实例放在ActionContext中,因此需要用OGNL标签用“#objectName”的方式取值。具体请看jsp页面:
<%@ page language="java"contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s"uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type"content="text/html; charset=UTF-8"> <title>控制标签</title> <style type="text/css"> table.demo{border-collapse: collapse;width: 400px;height: 300px;text-align: center;} table.demo th,td {padding: 0; border: 1px solid #000;} .odd{background-color:#13ABC2;} .even{background-color:#DED95B;}</style> </head> <body> <table class="demo"> <tr> <td>歌曲</td> <td>歌手</td> <td>专辑</td> <td>发布时间</td> </tr> <s:iterator value="songs"status="songsStatus"> <tr class="<s:if test="#songsStatus.odd == true">odd</s:if><s:else>even</s:else>" > <td><s:property value="title"/></td> <td><s:property value="singer"/></td> <td><s:property value="album"/></td> <td><s:property value="publicTime"/></td> </tr> </s:iterator> </table> </body> </html>
这个jsp页面通过<s:if />…<s:else />标签使偶数行背景是olive色,奇数行是yellow色。
发布项目在地址栏输入地址:http://localhost:8000/Struts2Tutorial/ifElseStatus,页面所示:
好了,就介绍这么多了,附上struts.xml的文件内容:
<action name="iteratorSongs" class="com.iman.action.ControlAction" method="iteratorSongs"> <result name="success">/control/iteratorTag.jsp</result> </action> <action name="ifElseStatus" class="com.iman.action.ControlAction" method="iteratorSongs"> <result name="success">/control/ifElseTag.jsp</result> </action>
这是本人学习的结果,欢迎转载,欢迎交流,但转载务必给出本文章的链接地址:http://blog.csdn.net/youqishini/article/details/7072068,谢谢~