struts2 iterator标签

来源:互联网 发布:mac如何给iphone刷机 编辑:程序博客网 时间:2024/04/30 23:45
iterator标签主要是用于迭代输出集合元素,如list set map 数组等,在使用<s:iterator/>标签的时候有三个属性值得我们关注

1. value属性:可选的属性,value属性是指一个被迭代的集合,使用ognl表达式指定,如果为空的话默认就是ValueStack栈顶的集合.
2.id属性:可选属性, 是指集合元素的id
3.status属性:可选属性,该属性在迭代时会产生一个IteratorStatus对象,该对象可以判断当前元素的位置,包含了以下属性方法:

int getCount(); 迭代元素个数

int getIndex(); 迭代元素当前索引

boolean getFirst(); 是否为第一个

boolean getEven(); 是否为偶

boolean getLast(); 是否最后一个

bolean getOdd();   是否为奇

由于iteratorstatus对象并不是ognl的根对象因此访问需要加上 #访问如下例子:

<s:iterator value=”{’dd’,'bb’,'cc’}” status=”st”>

   <s:if test=”#st.odd”>

      <s:property value=”#st.index”/>

   </s:if>

</s:iterator>

 

此外:iterator也可以迭代map对象,map对象中有几对key-value 就迭代几次,分别使用
<s:property value=”key”/>
<s:property value=”value”/>

示例:

Action类:

package com.demo.action;

import java.util.*;

import com.opensymphony.xwork2.ActionSupport;

public class iteratorTag extends ActionSupport {

private List myList;

private Map myMap;

public List getMyList() {

return myList;

}

public void setMyList(List myList) {

this.myList = myList;

}

public Map getMyMap() {

return myMap;

}

public void setMyMap(Map myMap) {

this.myMap = myMap;

}

public String execute() throws Exception{

myList=new ArrayList();

myList.add("第一个元素");

myList.add("第二个元素");

myList.add("第三个元素");

myMap=new HashMap();

myMap.put("key1", "第一个元素");

myMap.put("key2", "第二个元素");

myMap.put("key3", "第三个元素");

return SUCCESS;

}

}

iteratorTags.jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags" %>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP 'iteratorTags.jsp' starting page</title>

    

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    `

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

<title>Struts2 的 iterator 标签使用示例</title>


  </head>

  

  <body>

    

    <h1><span style="background-color:#FFFFcc">iterator 标签示例</span></h1>

    <h2>显示List </h2>

    

    <table>

     <s:iterator value="{'第一个元素','第二个元素'}" status="st">

     <tr>

     <td><s:property value="#st.getIndex()"/></td>

     <td><s:property/></td>

     </tr>

     </s:iterator>

    </table>

    

    <h2>显示List属性</h2>

    <table>

     <s:iterator value="myList" status="st">

     <tr>

     <td><s:property value="#st.getIndex()"/></td>

     <td><s:property/></td>

     </tr>

     </s:iterator>

    </table>

    

    <h2>显示Map</h2>

    <table>

     <s:iterator value="#{'key':'第一个元素','key2':'第二个元素' }" status="st">

     <tr>

     <td><s:property value="#st.getIndex()" /></td>

     <td><s:property /></td>

     </tr>

     </s:iterator>

    </table>

    

    <h2>显示Map属性</h2>

    <table>

     <s:iterator value="myMap" status="st">

     <tr>

     <td><s:property value="#st.getIndex()"/></td>

     <td><s:property/></td>

     </tr>

     </s:iterator>

    </table>

  </body>

</html>

http://localhost:8080/struts2/iteratorTag.action结果页面

iterator 标签示例

显示List

0第一个元素1第二个元素

显示List属性

0第一个元素1第二个元素2第三个元素

显示Map

0key=第一个元素1key2=第二个元素

显示Map属性

key3=第三个元素key2=第二个元素key1=第一个元素



0 0
原创粉丝点击