struts2核心(3)——访问ValueStack
来源:互联网 发布:美国淘宝网站 编辑:程序博客网 时间:2024/06/08 09:14
转载请注明:http://blog.csdn.net/uniquewonderq
访问ValueStack:
问题描述:在页面上,使用Struts2标签和OGNL表达式观察ValueStack的结构,并访问ValueStack中的栈对象和Context对象的数据,包括如下内容:
1.观察ValueStack的结构
2.输出栈顶内容
3.输出Context对象中的数据
4.遍历集合
5.按数字迭代
步骤一:
在hello.jsp上增加<s:debug/>标签,通过标签来观察ValueStack结构,代码如下;
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><%@taglib uri="/struts-tags" prefix="s" %><html> <head> </head> <body> <h1>演示OGNL表达式</h1> <%-- <h1>1.基本属性</h1> <h2>ID:<s:property value="id"/></h2> <h2>Name:<s:property value="name"/></h2> <h1>2.实体对象</h1> <h2>用户名:<s:property value="user.userName"/></h2> <h2>密码:<s:property value="user.password"/></h2> <h1>3.数组和集合</h1> <h2>数组中的城市:<s:property value="cityArray[1]"/></h2> <h2>集合中的城市:<s:property value="cityList[1]"/></h2> <h1>4.Map</h1> <h2>Map中的城市人口:<s:property value="cityMap.beijing"/></h2> <h1>5.访问时运算</h1> <h2>My Name:<s:property value="'My name is '+name"/></h2> <h2>去哪:<s:property value="cityArray[1]+',我来了!'"/></h2> <h1>6.访问时调用方法</h1> <h2>MY NAME:<s:property value="name.toUpperCase()"/></h2> <h1>7.创建集合</h1> <h2>创建集合:<s:property value="{'a','b','c'}"/></h2> <h2>集合类型:<s:property value="{'a','b','c'}.getClass().getName()"/></h2> <h1>8.创建Map</h1> <h2>创建Map:<s:property value="#{'mm':'MM','nn':'NN'}"/></h2> <h2>Map类型:<s:property value="#{'mm':'MM','nn':'NN'}.getClass().getName()"/></h2> --%> <!-- ValueStack示例 --> <h1>1.观察ValueStack结构</h1> <h2><s:debug/></h2> </body></html>
重新部署项目并启动tomcat后,访问strutsDay02,效果如下:
点击debug按钮后,效果如下:
步骤二:输出栈顶内容
在hello.jsp中,使用Struts2标签输出栈顶的内容,代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><%@taglib uri="/struts-tags" prefix="s" %><html> <head> </head> <body> <h1>演示OGNL表达式</h1> <%-- <h1>1.基本属性</h1> <h2>ID:<s:property value="id"/></h2> <h2>Name:<s:property value="name"/></h2> <h1>2.实体对象</h1> <h2>用户名:<s:property value="user.userName"/></h2> <h2>密码:<s:property value="user.password"/></h2> <h1>3.数组和集合</h1> <h2>数组中的城市:<s:property value="cityArray[1]"/></h2> <h2>集合中的城市:<s:property value="cityList[1]"/></h2> <h1>4.Map</h1> <h2>Map中的城市人口:<s:property value="cityMap.beijing"/></h2> <h1>5.访问时运算</h1> <h2>My Name:<s:property value="'My name is '+name"/></h2> <h2>去哪:<s:property value="cityArray[1]+',我来了!'"/></h2> <h1>6.访问时调用方法</h1> <h2>MY NAME:<s:property value="name.toUpperCase()"/></h2> <h1>7.创建集合</h1> <h2>创建集合:<s:property value="{'a','b','c'}"/></h2> <h2>集合类型:<s:property value="{'a','b','c'}.getClass().getName()"/></h2> <h1>8.创建Map</h1> <h2>创建Map:<s:property value="#{'mm':'MM','nn':'NN'}"/></h2> <h2>Map类型:<s:property value="#{'mm':'MM','nn':'NN'}.getClass().getName()"/></h2> --%> <!-- ValueStack示例 --> <h1>1.观察ValueStack结构</h1> <h2><s:debug/></h2> <h1>2.栈顶内容如下:</h1> <h2>Stack Top:<s:property/></h2> </body></html>
运行效果图如下:
步骤三:输出Context对象中的内容
hello.jsp中,在struts2显示标签上使用OGNL输出ValueStack中Context对象的值,表达式格式为:“#key”,代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><%@taglib uri="/struts-tags" prefix="s" %><html> <head> </head> <body> <h1>演示OGNL表达式</h1> <%-- <h1>1.基本属性</h1> <h2>ID:<s:property value="id"/></h2> <h2>Name:<s:property value="name"/></h2> <h1>2.实体对象</h1> <h2>用户名:<s:property value="user.userName"/></h2> <h2>密码:<s:property value="user.password"/></h2> <h1>3.数组和集合</h1> <h2>数组中的城市:<s:property value="cityArray[1]"/></h2> <h2>集合中的城市:<s:property value="cityList[1]"/></h2> <h1>4.Map</h1> <h2>Map中的城市人口:<s:property value="cityMap.beijing"/></h2> <h1>5.访问时运算</h1> <h2>My Name:<s:property value="'My name is '+name"/></h2> <h2>去哪:<s:property value="cityArray[1]+',我来了!'"/></h2> <h1>6.访问时调用方法</h1> <h2>MY NAME:<s:property value="name.toUpperCase()"/></h2> <h1>7.创建集合</h1> <h2>创建集合:<s:property value="{'a','b','c'}"/></h2> <h2>集合类型:<s:property value="{'a','b','c'}.getClass().getName()"/></h2> <h1>8.创建Map</h1> <h2>创建Map:<s:property value="#{'mm':'MM','nn':'NN'}"/></h2> <h2>Map类型:<s:property value="#{'mm':'MM','nn':'NN'}.getClass().getName()"/></h2> --%> <!-- ValueStack示例 --> <h1>1.观察ValueStack结构</h1> <h2><s:debug/></h2> <h1>2.栈顶内容如下:</h1> <h2>Stack Top:<s:property/></h2> <h1>3.输出context对象中的数据:</h1> <h2>context:<s:property value="#action.user.userName"/></h2> </body></html>
步骤四:遍历集合
在HelloAction中追加集合属性并实例化、同时提供get、set方法,在业务方法中初始化集合的数据,代码如下:
package action;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import entity.Emp;import entity.User;public class HelloAction {//基本类型private Integer id=1;private String name="zhangsan";//实体对象private User user=new User();//数组和集合private String[] cityArray=new String[]{"beijing","shanghai","guangzhou"};private List<String> cityList=new ArrayList<String>();//Mapprivate Map<String, String> cityMap=new HashMap<String, String>();//员工集合private List<Emp> emps=new ArrayList<Emp>();public String sayHello(){//初始化实体对象user.setUserName("zhangsan");user.setPassword("123456");//初始化集合数据cityList.add("shenzhen");cityList.add("congqing");cityList.add("qingdao");//初始化Map数据cityMap.put("beijing", "2300万人口");cityMap.put("shanghai", "2000万人口");cityMap.put("guangzhou", "1800万人口");//初始化员工数据Emp e1=new Emp();e1.setEmpName("刘备");e1.setSalary(8000.0);emps.add(e1);Emp e2=new Emp();e2.setEmpName("关羽");e2.setSalary(6000.00);emps.add(e2);Emp e3=new Emp();e3.setEmpName("张飞");e3.setSalary(5000.0);emps.add(e3);return "success";}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public User getUser() {return user;}public void setUser(User user) {this.user = user;}public String[] getCityArray() {return cityArray;}public void setCityArray(String[] cityArray) {this.cityArray = cityArray;}public List<String> getCityList() {return cityList;}public void setCityList(List<String> cityList) {this.cityList = cityList;}public Map<String, String> getCityMap() {return cityMap;}public void setCityMap(Map<String, String> cityMap) {this.cityMap = cityMap;}public List<Emp> getEmps() {return emps;}public void setEmps(List<Emp> emps) {this.emps = emps;}}
在hello.jsp中,使用迭代标签遍历上述集合属性,并输出结合中变量emp的属性值,代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><%@taglib uri="/struts-tags" prefix="s" %><html> <head> </head> <body> <h1>演示OGNL表达式</h1> <%-- <h1>1.基本属性</h1> <h2>ID:<s:property value="id"/></h2> <h2>Name:<s:property value="name"/></h2> <h1>2.实体对象</h1> <h2>用户名:<s:property value="user.userName"/></h2> <h2>密码:<s:property value="user.password"/></h2> <h1>3.数组和集合</h1> <h2>数组中的城市:<s:property value="cityArray[1]"/></h2> <h2>集合中的城市:<s:property value="cityList[1]"/></h2> <h1>4.Map</h1> <h2>Map中的城市人口:<s:property value="cityMap.beijing"/></h2> <h1>5.访问时运算</h1> <h2>My Name:<s:property value="'My name is '+name"/></h2> <h2>去哪:<s:property value="cityArray[1]+',我来了!'"/></h2> <h1>6.访问时调用方法</h1> <h2>MY NAME:<s:property value="name.toUpperCase()"/></h2> <h1>7.创建集合</h1> <h2>创建集合:<s:property value="{'a','b','c'}"/></h2> <h2>集合类型:<s:property value="{'a','b','c'}.getClass().getName()"/></h2> <h1>8.创建Map</h1> <h2>创建Map:<s:property value="#{'mm':'MM','nn':'NN'}"/></h2> <h2>Map类型:<s:property value="#{'mm':'MM','nn':'NN'}.getClass().getName()"/></h2> --%> <!-- ValueStack示例 --> <h1>1.观察ValueStack结构</h1> <h2><s:debug/></h2> <h1>2.栈顶内容如下:</h1> <h2>Stack Top:<s:property/></h2> <h1>3.输出context对象中的数据:</h1> <h2>context:<s:property value="#action.user.userName"/></h2> <h1>4.遍历集合</h1> <h2> <!-- 迭代标签,遍历集合 --> <s:iterator value="emps"> <!-- 迭代过程中,栈顶为Emp对象,写OGNL输出其属性值 --> 员工:<s:property value="empName"/>, 工资为:<s:property value="salary"/><br/> </s:iterator> </h2> </body></html>
运行效果如下:
步骤五:按数字迭代
在HelloAction中追加迭代的起止值并实例化,同时提供get、set、方法,代码如下:
package action;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import entity.Emp;import entity.User;public class HelloAction {//基本类型private Integer id=1;private String name="zhangsan";//实体对象private User user=new User();//数组和集合private String[] cityArray=new String[]{"beijing","shanghai","guangzhou"};private List<String> cityList=new ArrayList<String>();//Mapprivate Map<String, String> cityMap=new HashMap<String, String>();//员工集合private List<Emp> emps=new ArrayList<Emp>();//迭代数字的起止值private Integer from=1;private Integer to=3;public String sayHello(){//初始化实体对象user.setUserName("zhangsan");user.setPassword("123456");//初始化集合数据cityList.add("shenzhen");cityList.add("congqing");cityList.add("qingdao");//初始化Map数据cityMap.put("beijing", "2300万人口");cityMap.put("shanghai", "2000万人口");cityMap.put("guangzhou", "1800万人口");//初始化员工数据Emp e1=new Emp();e1.setEmpName("刘备");e1.setSalary(8000.0);emps.add(e1);Emp e2=new Emp();e2.setEmpName("关羽");e2.setSalary(6000.00);emps.add(e2);Emp e3=new Emp();e3.setEmpName("张飞");e3.setSalary(5000.0);emps.add(e3);return "success";}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public User getUser() {return user;}public void setUser(User user) {this.user = user;}public String[] getCityArray() {return cityArray;}public void setCityArray(String[] cityArray) {this.cityArray = cityArray;}public List<String> getCityList() {return cityList;}public void setCityList(List<String> cityList) {this.cityList = cityList;}public Map<String, String> getCityMap() {return cityMap;}public void setCityMap(Map<String, String> cityMap) {this.cityMap = cityMap;}public List<Emp> getEmps() {return emps;}public void setEmps(List<Emp> emps) {this.emps = emps;}public Integer getFrom() {return from;}public void setFrom(Integer from) {this.from = from;}public Integer getTo() {return to;}public void setTo(Integer to) {this.to = to;}}
在hello.jsp中,使用迭代标签按照HelloAction中的起止值,循环,并输出循环变量的值代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><%@taglib uri="/struts-tags" prefix="s" %><html> <head> </head> <body> <h1>演示OGNL表达式</h1> <%-- <h1>1.基本属性</h1> <h2>ID:<s:property value="id"/></h2> <h2>Name:<s:property value="name"/></h2> <h1>2.实体对象</h1> <h2>用户名:<s:property value="user.userName"/></h2> <h2>密码:<s:property value="user.password"/></h2> <h1>3.数组和集合</h1> <h2>数组中的城市:<s:property value="cityArray[1]"/></h2> <h2>集合中的城市:<s:property value="cityList[1]"/></h2> <h1>4.Map</h1> <h2>Map中的城市人口:<s:property value="cityMap.beijing"/></h2> <h1>5.访问时运算</h1> <h2>My Name:<s:property value="'My name is '+name"/></h2> <h2>去哪:<s:property value="cityArray[1]+',我来了!'"/></h2> <h1>6.访问时调用方法</h1> <h2>MY NAME:<s:property value="name.toUpperCase()"/></h2> <h1>7.创建集合</h1> <h2>创建集合:<s:property value="{'a','b','c'}"/></h2> <h2>集合类型:<s:property value="{'a','b','c'}.getClass().getName()"/></h2> <h1>8.创建Map</h1> <h2>创建Map:<s:property value="#{'mm':'MM','nn':'NN'}"/></h2> <h2>Map类型:<s:property value="#{'mm':'MM','nn':'NN'}.getClass().getName()"/></h2> --%> <!-- ValueStack示例 --> <h1>1.观察ValueStack结构</h1> <h2><s:debug/></h2> <h1>2.栈顶内容如下:</h1> <h2>Stack Top:<s:property/></h2> <h1>3.输出context对象中的数据:</h1> <h2>context:<s:property value="#action.user.userName"/></h2> <h1>4.遍历集合</h1> <h2> <!-- 迭代标签,遍历集合 --> <s:iterator value="emps"> <!-- 迭代过程中,栈顶为Emp对象,写OGNL输出其属性值 --> 员工:<s:property value="empName"/>, 工资为:<s:property value="salary"/><br/> </s:iterator> </h2> <h1>5.循环数字</h1> <h2> <!-- 迭代标签,按数字迭代 --> <s:iterator begin="from" end="to" var="p"> <!-- 迭代过程中,栈顶为数字,但不能以数字为root写OGNL。 此时可以声明变量p,通过Context对象实现对变量的访问 --> <s:property value="#p"/>, </s:iterator> </h2> </body></html>
运行效果:
0 0
- struts2核心(3)——访问ValueStack
- struts2核心(一)——ONGL以及ValueStack
- Struts2——(3)ValueStack(值栈)
- javascript中访问struts2 ValueStack中的数据
- Struts2的ValueStack(值栈)
- struts2——ValueStack,模块包含,默认action!!!
- struts2中几个核心的组件ActionContext、ValueStack、OGNL表达式
- struts2中几个核心的组件ActionContext、ValueStack、OGNL表达式
- Struts2-----ValueStack
- Struts2:ValueStack
- struts2 valuestack
- Struts2 ValueStack
- Struts2--ValueStack
- struts2中OGNL和 ValueStack(一)
- struts2中OGNL和 ValueStack(一)
- struts2中OGNL和 ValueStack(二)
- struts2之valueStack(OGNL) (总结)
- struts2中OGNL和 ValueStack(一)
- String,StringBuffer与StringBuilder的区别详解
- 现代的代码审查实践对软件质量影响的实证研究
- iOS平台游戏安全之IPA破解原理及防御(第三弹)
- 数据结构之String
- 用户输入M,N值,从1至N开始顺序循环数数,每数到M输出该数值,直至全部输出。写出C程序。
- struts2核心(3)——访问ValueStack
- 2015071704 - 理想
- 从request获取工程各种路径总结
- objcetive-c学习笔记之 字符串
- iOS技术点杂记--有一天我也会成为大牛
- keil编译后生成的M51文件解析
- SQL数据库-TSQL语法实例介绍
- 【leetcode c++】83 Remove Duplicates from Sorted List
- 求旋转数组的最小值