struts2的iterator标签总结(精)

时间:2024.5.2

一、遍历数组

<%

String[] arr_str = new String[]{"a","b","c","d"}; request.setAttribute("arr_str" , arr_str); %>

<!--遍历一个数组-->

<s:set name="arr_str" value="#request.arr_str"/> <s:iterator value="#arr_str" id="itStr">

<s:property value="itStr" /> </s:iterator>

<br/><hr/>

二、遍历一个list,里面存放的是bean

public class ActionInterest extends ActionSupport {

/** * */ public String execute() throws Exception { } inList = new ArrayList(); public void setInList(ArrayList inList) { } this.inList = inList; public ArrayList getInList() { } return inList; private ArrayList inList; Interest in1 = new Interest("swim","swim is good"); Interest in2 = new Interest("run","run is good"); inList.add(in1); inList.add(in2); return SUCCESS;

JSP:

class Interest{(把它单独提出去效果是一样的,即导入) } private String fav ; private String desc; public String getFav() { } public void setFav(String fav) { } public String getDesc() { } public void setDesc(String desc) { } public Interest(String fav,String desc){ } this.desc = desc; this.fav = fav; this.desc = desc; return desc; this.fav = fav; return fav;

<!-- 遍历一个list,里面的内容是一个bean --> <s:iterator value="inList">

<s:property value="fav"/>(可以直接读取到) <s:property value="desc"/>

<br/>

</s:iterator>

如果list里面放入的不是bean,那更简单,

<s:iterator value="inList">

<s:property />

</s:iterator>

就可以了。

三、遍历MAP

(1) map<string,string>

<!-- 遍历一个Map,里面的内容是String,String -->

<s:iterator value="map">

<s:property value="key"/>

<s:property value="value"/>

<br/>

</s:iterator>

(2) map<string,bean>

<!-- 遍历一个Map,里面的内容是String,bean -->

<s:iterator value="map">

<s:property value="key"/> <s:property value="value.fav"/>,<s:property value="value.desc"/> <br/>

</s:iterator>

(3) map<string,list>

<!-- 遍历一个Map,里面的内容是String,list -->

<br/><hr/>

<s:iterator value="map" id="column">

<s:property value="key"/>

<s:iterator value="#column.value"> <s:property value="fav"/>, <s:property value="comment"/><br/> </s:iterator>

</s:iterator>

后台Action

(4) map<string,string[]> map.put("1", inList); map.put("2", inList2);

<s:iterator value="arrayMap" id="column"> <tr>

<td><s:property value="#column"/></td> <td><s:property value="value[0]"/></td> <td><s:property value="value[1]"/></td> <td><s:property value="value[2]"/></td> <td><s:property value="value[3]"/></td> <td><s:property value="value[4]"/></td> </tr>

</s:iterator>


第二篇:struts2 标签iterator和Bean的混合使用


Struts 标签<s:bean>和<s: iterator > 的混合使用

网上找了很多例子,不过都不是自己想要的iterator遍历Bean的实现,没办法,自己摸索了很久终于找出了一点门路:

首先struts标签的使用要在jsp网页头顶上加入:

<%@ taglib prefix="s" uri="/struts-tags"%> <!--Struts2 标签库 --> <s:bean>的使用:

假设有个包JavaBean,包里有个类Hotsixphoto,类有属性ID,name,photo,browse四个。现在类里边除了四个属性的getter and setter方法,还有有个方法selectThing( ),这个方法返回的是一个ArrayList。

现在在jsp页面上要使用属性ID,name,photo,browse四个,首先声明: <s:bean name="JavaBean. HotSixphoto" id="pro"></s:bean> 这里name是用到的“包.类”;id是名称,没有实际意义,可以随便起。 <s: iterator >遍历Bean:

<s:iterator

status="st"> id="top6" value="%{#pro.selectThing()}"

id是名称,没有实际意义,可以随便起。Value是取值的意思,现在取得是上边Bean定义的bean的id(pro,这里pro已经是Hotsixphoto的一个对象了)调用其函数返回ArrayList对象,要注意上边value="%{#pro. selectThing( )}的“#”号,是不可以少的。Iterator标签就是有点奇怪的地方:上边两句已经用selectThing()获得了返回的ArrayList对象。要输出对象的各个属性不需要用id去调用而是直接用属性名就行了的。例如取ID,name,photo,browse四个属性语句如下:

<s:property value="%{name}" />

<s:property value="%{ID}" />

<s:property value="%{ photo }" />

<s:property value="%{browse}" />

package JavaBean;

import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList;

public class hotSixphoto {

public void setPrice(String price) { } this.price = price; public String getPrice() { } return price; public void setName(String name) { } this.name = name; public String getName() { } return name; public void setID(String id) { } ID = id; private String ID; private String name; private String price; private String browse;//import java.io.File; Statement stmt = null; ResultSet rs= null; Connetion conn=new Connetion(); public String getID() { } return ID;

public String getBrowse() { } return browse; public void setBrowse(String browse) { } this.browse = browse;

//===================================================================

public ArrayList selectThing( ) throws Exception{ ArrayList al=new ArrayList();

?? return al;

}//select6boo

}// hotSixphoto

Jsp页面代码:

<s:bean name="JavaBean.hotSixphoto" id="pro">

<s:iterator id="top6" value="%{#pro.select6book()}" status="st">

<td width="200">

<table width="200" border="1" align="center">

<tr>

<p style="font-size:12px">名称:<s:property value="%{name}" /></p> <p>图像:

<s:a href="AA.jsp"><img src="<s:property value="%{browse}"/> " width="100" height="60" /></s:a>

</p>

<p>价钱:¥<s:property value="%{price}" /></p>

</tr>

</table>

</td>

</s:iterator>

</s:bean>

输出结果:

struts2标签iterator和Bean的混合使用

更多相关推荐:
The Ultimate Trainers

AfterIreadthisbookIthinkIshouldlearnsomethingusefulandoptimisticfromJakethefirstcharacterinthestoryAlthoughtheultim...

The summary of the ultimate trainers

ThesummaryoftheultimatetrainersThestoryisaboutaboycalledJakeandtheultimatetrainersJakeandAnnaaregoodfriendsOnedayaf...

The ultimate trainers

ByTianyeZhangThisstoryisaboutaboynamedJakewholikedrunningverymuchtooktheperfecttrainershesawontopofacardboxnexttoap...

Treasure Island金银岛读书笔记英文

RobertLouisBalfourStevensonwasaScottishnovelistpoetessayistandtravelwriterHismostfamousworksareTreasureIslandKidnap...

Treasure island金银岛读书报告

TreasureIslandThestoryistoldbyJimHawkinshimselfwhosemotherkepttheAdmiralBenbowInnAnoldseadogcomestothispeacefulinno...

金银岛_英文读后感_treasure_islad

BookReportofTreasureIsland1AbouttheAuthorRobertLouisStevensonwasaScottishnovelistessayistandpoetwhocontributedsever...

Treasure island 读后感

学号20xx09410117专业师范英语姓名刘欢TreasureIslandPlotintroductionJimwasatenyearoldboyHisfamilyhadaBenbowinntoearnthe...

金银岛 英文读后感 treasure islad

BookReportofTreasureIsland1AbouttheAuthorRobertLouisStevensonwasaScottishnovelistessayistandpoetwhocontributedsever...

treasure island

TreasurelandTreasurelandpublishedin1882istheadventurestoryparexcellenceanditmadethewriter39sreputationaso...

Treasure Island读后感

TreasureIslandOnthisNationalDayholidayIreadabookcalledTreasureIslandThisstorydescribestheprocessofatreasureTheprota...

treasure island 英语读书笔记

ReadingGuideHelenZBookTreasureIslandAuthorRobertLouisStevensonIntroductiontotheauthorRobertLouisStevensonwasborninE...

Doc4简爱读后感 英文版

WhatdoIwriteaboutyouJaneWordsfallshortwhenItrytoJaneyouaresomuchapartofmeasIamyoursYouaresomuchapartofwomenwholived...

the ultimate trainers读后感(3篇)