您的位置:首页 > 编程语言 > Java开发

struts标签中<s:iterator>的使用

2012-05-20 22:19 561 查看
struts中的Iterator标记实现迭代控制,它主要有三个参数value,id,status

value的值表示原始的数组或集合,id的值为当前迭代变量,status的值表示当前迭代的状态变量

使用的实例如下:

有实体类:

//路线

public class Path{

private Integer id; //记录编号

private Integer pathNum; //线路序号

private String pathName; //景点名称

private String introduce; //景点介绍

private Integer price; //价格

private String servicePhone; //客服热线

private String picture; //照片的路径

}

PathAction类:

//旅游路线

public class PathAction extends ActionSupport{

PathServiceImpl pathService;

private List<Path> pathList1 = new ArrayList();

private List<Path> pathList2 = new ArrayList();

private Integer pathId;

private Path myPath;

//获得路线一

public String getPath1(){

this.pathList1 = pathService.getPathByNum(1);

return "path1";

}

//获得路线二

public String getPath2(){

this.pathList2 = pathService.getPathByNum(2);

return "path2";

}

//获得某个景点的详细信息

public String description(){

this.myPath = pathService.getPathById(pathId);

return "descript";

}

public PathServiceImpl getPathService() {

return pathService;

}

public void setPathService(PathServiceImpl pathService) {

this.pathService = pathService;

}

public List<Path> getPathList1() {

return pathList1;

}

public List<Path> getPathList2() {

return pathList2;

}

public void setPathList1(List<Path> pathList1) {

this.pathList1 = pathList1;

}

public void setPathList2(List<Path> pathList2) {

this.pathList2 = pathList2;

}

public Integer getPathId() {

return pathId;

}

public void setPathId(Integer pathId) {

this.pathId = pathId;

}

public Path getMyPath() {

return myPath;

}

public void setMyPath(Path myPath) {

this.myPath = myPath;

}

}

jsp中展示表格的代码:

<table border="1" width="550">

<tr>

<td width="10%" align="center" height="16">序号</td>

<td width="20%" align="center" height="16">名称</td>

<td width="20%" align="center" height="16">价格(元)</td>

<td width="30%" align="center" height="16">客服热线</td>

<td width="10%" align="center" height="16">详情</td>

<td width="10%" align="center" height="16">预订</td>

</tr>

<%int cnt = 1; %>

<s:iterator value="pathList1" id="pt1" status="st1">

<tr>

<td width="10%" align="center" height="16"><%=cnt++ %></td>

<td width="20%" align="center" height="16">${pt1.pathName}</td>

<td width="20%" align="center" height="16">${pt1.price}</td>

<td width="30%" align="center" height="16">${pt1.servicePhone}</td>

<td width="10%" align="center" height="16">

<A class=nav href="path_description.action?pathId=${pt1.id}">查看</A></td>

<td width="10%" align="center" height="16">

<A class=nav href="order_detail.action?pathId=${pt1.id}&userId=<%=session.getAttribute("userId")%>">预订</A></td>

</tr>

</s:iterator>

</table>

struts.xml文件:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>

<constant name="struts.i18n.encoding" value="UTF-8"/>

<constant name="struts.devMode" value="true"/>

<package name="default" extends="struts-default,jasperreports-default" namespace="/">

<action name="path_*" class="pathAction" method="{1}">

<result name="path1">num1.jsp</result>

</action>

</package>

</struts>

访问path_getPath1.action即可得到路线一的列表。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: