属性名 | 是否必需 | 默认值 | 类型 | 说明介绍 |
---|---|---|---|---|
test |
true |
—— |
boolean |
表达式,支持OGNL语言 |
package controller; public class ShowSex { private String sex; public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String execute() { this.setSex("male"); return "showsex"; } }在jsp页面中使用s:if标签输出性别:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <%@ taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> <s:if test="sex=='male'">male</s:if> <s:elseif test="sex==female">female</s:elseif> <s:else>...</s:else> </body> </html>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtd/struts-2.0.dtd"> <struts> <package name="Maybe" extends="struts-default"> <action name="showsex" class="controller.ShowSex"> <result name="showsex">/showsex.jsp</result> </action> </package> </struts>
属性名 | 是否必需 | 默认值 | 类型 | 说明介绍 |
---|---|---|---|---|
status | false | false | String | 如果指定status属性,则创建一个IteratorStatus类的实例 并放到值栈中 |
value | false | —— | String | 被循环的集合包括List,Set,数组和Map集合
|
var |
false | —— | String | 集合中的每一个元素,并且放到值栈中
|
begin |
false | —— | int | 起始的索引
|
end |
false | —— | int | 结束的索引
|
package controller; public class ShowStringArray { private String[] studentList = new String[3]; public String[] getStudentList() { return studentList; } public void setStudentList(String[] studentList) { this.studentList = studentList; } public String execute() { studentList[0] = "niujiabin"; studentList[1] = "maybe"; studentList[2] = "GossipGo"; return "showstringarray"; } }
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <%@ taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> <s:iterator value="studentList" var = "studentName"> <s:property value="studentName"/> </s:iterator> </body> </html>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtd/struts-2.0.dtd"> <struts> <package name="Maybe" extends="struts-default"> <action name="showstringarray" class="controller.ShowStringArray"> <result name="showstringarray">/showstringarray.jsp</result> </action> </package> </struts>运行结果:
package controller; import java.util.ArrayList; import bean.StudentInfo; public class ShowListJavaBean { private ArrayList listJavaBean = new ArrayList(); public ArrayList getListJavaBean() { return listJavaBean; } public void setListJavaBean(ArrayList listJavaBean) { this.listJavaBean = listJavaBean; } public String execute() { StudentInfo student1 = new StudentInfo(); StudentInfo student2 = new StudentInfo(); StudentInfo student3 = new StudentInfo(); student1.setAge(20); student1.setName("niujiabin"); student2.setAge(21); student2.setName("maybe"); student3.setAge(21); student3.setName("GossipGo"); listJavaBean.add(student3); listJavaBean.add(student2); listJavaBean.add(student1); return "showlistjavabean"; } }javaBean 类实体:
package bean; public class StudentInfo { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> 第一种写法: <br> <s:iterator value="listJavaBean"> 姓名:<s:property value="name"/> 年龄:<s:property value="age"/> </s:iterator> <br> 第二种写法: <br> <s:iterator valu="listJavaBean" var="studentJavaBean"> 姓名: <s:property value="#studentJavaBean.name"/> 年龄:<s:property value="#studentJavaBean.age"/> </s:iterator> </body> </html>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtd/struts-2.0.dtd"> <struts> <package name="Maybe" extends="struts-default"> <action name="showlistjavabean" class="controller.ShowListJavaBean"> <result name="showlistjavabean">/showlistjavabean.jsp</result> </action> </package> </struts>