<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@taglib prefix="s" uri="/struts-tags"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>遍历器</title> </head> <body> <center> <!-- 遍历器标签 status为遍历变量 --> <table border="1px" cellpadding ="0" cellspacing="0"> <s:iterator var="name" value="{'Html','Java','C++','JSP'}" status="st"> <s:if test="#st.odd"> <tr style="background-color:red;"> <td><s:property value="name"/></td> </tr> </s:if> <s:else> <tr style="background-color:green;"> <td><s:property value="name"/></td> </tr> </s:else> </s:iterator> </table> <br/> <!-- bean标签的使用 需要添加''表明是字符串--> <s:bean name="Bean.user" var="usr"> <s:param name="name" value="'tom'"/> <s:param name="age" value="20"/> </s:bean> <s:property value="#usr.name"/> </center> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@taglib prefix="s" uri="/struts-tags"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>输出集合的值</title> </head> <body> <center> <!-- 使用append标签将bean中得list添加到页面并用iterator遍历出来。存在Action中的数据不需要加#,页面数据需呀添加# --> <s:append var="myList"> <s:param value="%{list1}"></s:param> </s:append> <s:iterator value="%{#myList}"> <s:property/> </s:iterator> <br/> <!-- 控制标签 --> <s:if test="'A' in {'A','B'}"> In </s:if> <s:else> Not In </s:else> <br/> <!-- generator标签将集合分割,按separator分割。 --> <!-- count是输出多少个元素 --> <s:generator count="2" separator="," val="%{'aaa,bbb,ccc,ddd'}"> <s:iterator> <s:property/> </s:iterator> </s:generator> </center> </body> </html>
import java.util.ArrayList; import com.opensymphony.xwork2.ActionSupport; public class ListAction extends ActionSupport{ private ArrayList<Integer> list1; public ArrayList<Integer> getList1() { return list1; } public void setList1(ArrayList<Integer> list1) { this.list1 = list1; } public String execute(){ list1=new ArrayList<Integer>(); list1.add(1); list1.add(2); list1.add(3); return SUCCESS; } }