1.在show.jsp页面
<%@ 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> <head> </head> <body style="font-size: 30px;"> 查看ValueStack信息: <br> <s:debug></s:debug> <h1> 访问root中的 </h1> <br> id属性: <s:property value="id" /> <br> time属性: <s:property value="time" /> <br> 使用date标签设定显示时间的格式 <br> 指定时间格式: <s:date name="time" format="yyyy-MM-dd hh:mm:ss" /> <h1> 访问Context中对象 </h1> 访问Session: <s:property value="#session.hello" /> <h1> 使用OGNL调用方法 </h1> Message的长度: <s:property value="message.length()" /> <br> OGNL调用静态的属性: <s:property value="@java.lang.Math@PI" /> <br> <!--@:代表@java.lang.Math--> <s:property value="@@PI" /> <br> <!-- 自定义静态属性建议不要在Action中进行设置 --> 获取自定义静态属性: <s:property value="@zx.day2.action.ShowAction@show" /> <s:property value="#show" /> <br> 使用OGNL进行运算: <s:property value="id+100>250" /> <br> 范文List中的数据 <br> list第一个对象的工资: <s:property value="emps[0].name" /> 的工资为 <s:property value="emps[0].salary" /> <br> list的长度: <s:property value="emps.size" /> <br> 判断集合是否为空: <s:property value="emps.isEmpty" /> <hr> <h1> set标签 </h1> <!-- request.setAttribuet(key,value); --> scope:request: <s:set var="uname" value="emps[0].name" scope="request"></s:set> <s:property value="#request.uname" /> <br> scope:action <s:set var="usalary" value="emps[0]" scope="action"></s:set> <s:property value="#usalary.salary" /> <h2> push标签 </h2> <s:push value="emps[0]"> 姓名:<s:property value="name" /> <br> 工资:<s:property value="salary" /> </s:push><br> <h2> bean标签 </h2> <s:bean name="zx.day2.pojo.Emp" var="e1"> <s:param name="name">zhangsan</s:param> <s:param name="salary">1222.0</s:param> <!-- value的值使用单引号表示字符串 <s:param name="name" value="'zhangsan'"/> <s:param name="salary" value="133"></s:param> --> </s:bean> 从页面创建的对象中获取数据<br> name:<s:property value="#e1.name"/><br> salary:<s:property value="#e1.salary"/> </body> </html>
2.在zx.day2.pojo包中创建Emp实体类 package zx.day2.pojo; public class Emp { private int id; private String name; private double salary; public Emp(int id, String name, double salary) { this.id = id; this.name = name; this.salary = salary; } public Emp() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } }
3.actionTest.jsp页面
<%@ 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> <head> </head> <body style="font-size: 30px;"> <s:action name="list" namespace="/" executeResult="true" ignoreContextParams="false"> </s:action> </body> </html>
4.在 iterator.jsp页面
<%@ 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> <head> </head> <body style="font-size: 30px;"> <h1> Iterator 迭代标签 </h1> <s:iterator var="e" value="emps" status="s"> <s:property value="#s.index" /> <s:property value="#s.count" /> <s:property value="#e.name" /> <s:property value="#e.salary" /> <br> </s:iterator> <h2> if/else/elseif </h2> <s:if test="emps.size>5"> 员工个数超过5个 </s:if> <s:else> 就几个,牛什么牛! </s:else> <br> 员工数组中有没有一个叫tom的 <br> 对集合进行过滤: <br> <s:if test="emps.{?#this.salary>10000}"> a123有 </s:if> <s:else> 没有Tom </s:else> <br> 工资大于800的员工 <br> <s:iterator var="e" value="emps.{?#this.salary>800}"> <s:property value="#e.salary" /> <br> </s:iterator> 工资大于800的第一个员工 <br> <s:iterator var="e" value="emps.{^#this.salary>800}"> <s:property value="#e.salary" /> <br> </s:iterator> <br> 工资大于800的第一个员工 <br> <s:iterator var="e" value="emps.{$#this.salary>800}"> <s:property value="#e.salary" /> <br> </s:iterator> <br> <!-- Include:<br> <s:include value="opt.jsp"></s:include> <br> URL:<a href="<s:url value="/login.action"/>">登陆</a> --> </body> </html>
5.在ShowAction中
package zx.day2.action; import java.util.ArrayList; import java.util.Date; import java.util.List; import zx.day2.pojo.Emp; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class ShowAction extends ActionSupport { /** * */ private int id; private Date time; private String message; private static String show = "haoren"; private List<Emp> emps; public String execute() { id = 100; time = new Date(); message = "1234567"; // 创建了一个Session ActionContext.getContext().getSession().put("hello", "hello OGNL"); emps = new ArrayList<Emp>(); emps.add(new Emp(1, "haoren", 2500)); emps.add(new Emp(2, "lily", 1500)); emps.add(new Emp(3, "jerry", 800)); emps.add(new Emp(4, "tom", 650)); return "success"; } public int getId() { return id; } public void setId(int id) { this.id = id; } public Date getTime() { return time; } public void setTime(Date time) { this.time = time; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public static String getShow() { return show; } public static void setShow(String show) { ShowAction.show = show; } public List<Emp> getEmps() { return emps; } public void setEmps(List<Emp> emps) { this.emps = emps; } }
6.在struts.xml文件中
<action name="show" class="zx.day2.action.ShowAction"> <result name="success">/show.jsp</result> </action> <action name="list" class="zx.day2.action.ShowAction"> <result name="success">/iterator.jsp</result> </action>