Struts2基础复习系列(4)
来源:互联网 发布:javascript 尚学堂 编辑:程序博客网 时间:2024/04/28 07:54
承接Struts2基础复习系列(3)
九、 值栈、OGNL和重要的struts标签
1. 值栈的结构
2. 值栈的操作
a. 对象栈,root
ActionContext.getContext.getValueStack().push(obj);放到栈顶ActionContext.getContext.getValueStack().add(int a,obj);放到对象栈指定位置ActionContext.getContext.getValueStack().peek();从对象栈栈顶取出元素,但不移除。ActionContext.getContext.getValueStack().pop();从对象栈栈顶弹出元素。
从上面的方法可以看出,基本都是对arrayList方法的调用,因此对于对象栈来说,由于其继承了arrayList,因此对于对象栈的操作方法,实际就是对arrayList的操作方法,也就是基本的数据结构,数组线性表。
b. map栈,_value
主要是对servletapi的访问,也就是:通ServletActionContext.getRequest().setAttribute()的方式进行数据的存储。类似的还有getSession,getApplication等。对于map栈用的比较多的也就是与几个域对象的访问了。
c. 对context的操作
context实质上就是一个map结构,或者说在map的基础进行了进一步封装,同样给我们提供的相拥的方法,猜也猜得出来,就是对map这种数据结构的操作啦。不外乎是,map.put(key,value),map.get(key)等和map这种数据结构相关的操作。
3. ognl
a. s:debug标签
查看对象栈,和context的数据存放信息。
源码:
public String addVs(){ Person person = new Person(); person.setId("21"); person.setName("FiShelly."); person.setSex("boy"); ValueStack vs = ActionContext.getContext().getValueStack(); vs.push(person); return "success"; }
查看下图,可以发现person的相关属性已经在栈顶出现。
源码:
public String addContext(){ Person person = new Person(); person.setId("21"); person.setName("FiShelly."); person.setSex("boy"); ActionContext.getContext().put("person", person); return "success"; }
可以看到,person对象呗保存到了context中。
context:
b. s:property标签
把一个对象放到对象栈中时,可以直接通过的value属性直接访问对象的属性名,如果对象栈中有两个同名属性,那么对象栈从栈顶找到栈底,当找到第一个后就会将那一个显示出来,而剩余的则不会被显示。
代码:
public String addSp(){ Person person = new Person(); person.setId("21"); person.setName("FiShelly."); person.setSex("boy"); ActionContext.getContext().getValueStack().push(person); return "success"; }
<s:proterty value="name"/>
同名属性情况:
源码:
public String addSpSame(){ Person person = new Person(); person.setId("21"); person.setName("FiShelly."); person.setSex("boy"); this.name = "Shelly"; ActionContext.getContext().getValueStack().push(person); return "success"; }
<s:property value="#person.name"/>
利用访问context中属性
如果是context中的(通过ActionContext.getCont().put方式加到context中),则需要在value中加#xxx或#request.xxx来进行访问,如果说是在context的map栈中,则需要#request.xxx进行访问。(参考之前讲过的,访问ServletApi中的三种方式的第一种,因此对于ActionContext.getCont().put方式同样会放到request域,因此用#request.xxx同样可以访问)
源码:
public String addContext(){ Person person = new Person(); person.setId("21"); person.setName("FiShelly."); person.setSex("boy"); ActionContext.getContext().put("person", person); return "success"; }
输出:<s:property value="#person.name"/><br> request输出:<s:property value="#request.person.name"/>
可以发现使用ActionContext.getContext().put(“person”, person);通过普通输出和request的方式输出都可以访问
通过ServletActionContext存取
源码:
public String addContextMap(){ Person person = new Person(); person.setId("21"); person.setName("FiShelly."); person.setSex("boy"); ServletActionContext.getRequest().setAttribute("person", person); return "success";} 输出:<s:property value="#person.name"/><br> request输出:<s:property value="#request.person.name"/>
可以发现普通输出已经没有结果,只有通过#request.person.name的形式才能出结果。
c. s:iterator标签
使用时需要指定value属性值,如果不指定则默认迭代对象栈栈顶元素。同时使用这个标签时需要记住一句话:当前迭代的元素在对象栈的栈顶,也就说不管你迭代什么元素都好,都需要让那个要迭代的元素在栈顶。如果在context中,可以通过#xxx的方式让这个元素到栈顶中。
在对象栈中进行迭代
源码:
public String itVsList(){ Person person1 = new Person(); person1.setId("21"); person1.setName("FiShelly."); person1.setSex("boy"); Person person2 = new Person(); person2.setId("21"); person2.setName("Shelly."); person2.setSex("grid"); Person person3 = new Person(); person3.setId("37"); person3.setName("Fish"); person3.setSex("grid"); List<Person> list = new ArrayList<Person>(); list.add(person1); list.add(person2); list.add(person3); ActionContext.getContext().getValueStack().push(list); return "success"; }
<s:iterator> <s:property value="name"/><br> </s:iterator> <s:debug></s:debug>
将map放到对象栈中进行迭代
源码:
public String itVsMap(){ Person person1 = new Person(); person1.setId("21"); person1.setName("FiShelly."); person1.setSex("boy"); Person person2 = new Person(); person2.setId("21"); person2.setName("Shelly."); person2.setSex("grid"); Person person3 = new Person(); person3.setId("37"); person3.setName("Fish"); person3.setSex("grid"); Map<String, Person> map = new HashMap<String, Person>(); map.put("person1", person1); map.put("person2", person2); map.put("person3", person3); ActionContext.getContext().getValueStack().push(map); return "success"; }<s:iterator> <s:property value="key"/> <s:property value="value.name"/><br></s:iterator>
注意在迭代map的时候,由于此时已经在栈顶了,那么获取map中的对象时候,跟jstl标签一样,通过key,value.xx的方式来进行获取。
迭代context中对象。
将list放到context中。
源码:
public String itContextList(){ Person person1 = new Person(); person1.setId("21"); person1.setName("FiShelly."); person1.setSex("boy"); Person person2 = new Person(); person2.setId("21"); person2.setName("Shelly."); person2.setSex("grid"); Person person3 = new Person(); person3.setId("37"); person3.setName("Fish"); person3.setSex("grid"); List<Person> list = new ArrayList<Person>(); list.add(person1); list.add(person2); list.add(person3); ActionContext.getContext().put("list", list); return "success"; }<s:iterator value="#list"> <s:property value="name"/><br></s:iterator>
将map放到context中
源码:
public String itContextMap(){ Person person1 = new Person(); person1.setId("21"); person1.setName("FiShelly."); person1.setSex("boy"); Person person2 = new Person(); person2.setId("21"); person2.setName("Shelly."); person2.setSex("grid"); Person person3 = new Person(); person3.setId("37"); person3.setName("Fish"); person3.setSex("grid"); Map<String, Person> map = new HashMap<String, Person>(); map.put("person1", person1); map.put("person2", person2); map.put("person3", person3); ActionContext.getContext().put("map", map); return "success"; }<s:iterator value="#map"> <s:property value="key"/> <s:property value="value.name"/><br></s:iterator>
对于放到map栈的与放到context的类似,在这里就不描述的,无非就是将value属性#map换成#request.map就是了。
对于一些复杂的迭代,也是同样的道理,只要记住当前迭代元素在栈顶即可,这样想的话无非就是多几重iterator标签而已。
- Struts2基础复习系列(4)
- Struts2基础复习系列(1)
- Struts2基础复习系列(2)
- Struts2基础复习系列(3)
- Struts2基础复习系列(5)
- struts2基础复习
- Java基础复习系列一
- Java基础复习系列二
- Java基础复习系列三
- Java基础复习系列四
- Java基础复习系列五
- 2017复习Java基础系列
- java基础复习--4
- struts2复习
- java基础复习--复习总结4
- Java知识点复习系列(4)
- Java基础复习(4)
- C++基础复习心得4
- 创建OpenGL窗口
- 【python】python中的多态与duck typing
- 【java】多线程
- JaveSE-JTable 表头、数据居中显示
- Swift:表格视图单元格多选
- Struts2基础复习系列(4)
- 取石子(一)
- CentOS 6.5 64 安装多个mysql 服务 -阿里云服务器ECS
- MyBatis多参数传递的四种方式
- poj 1320 佩尔方程递推
- 数组名与指针
- Android控件之Dialog (one)
- 3D Touch 使用心得(仿微信3D Touch)
- 某畅销理财书读书笔记