Struts中的OGNL与值栈

来源:互联网 发布:java注解反射应用实例 编辑:程序博客网 时间:2024/06/08 09:14

在ActionContext中,全部是Map集合主要包括六大对象、:
request、session、application、parameter、valuestack
值栈:
值栈是Struts2框架提供的一个存储区域,是一个柱结构(FILO),往往存储action实例,观察值栈中的内容:

在action中定义public String execute(){this.user = new User(222,"aaa");return "success"

主页中${user.id}<br>
${user.name}

在index.jsp中定义标签

<%@ taglib uri="/struts-tags" prefix="s"%>

在body中定义<s:debug/>可以查看值栈中的内容
在LoginAction中定义:

public String execute(){ValueStack vs=ActionContext.getContext().getValueStack()vs.push(new User("111","cccc");return "success";

在index.jsp中定义:

${id}<br>${name}

通过以上演示,值栈是一个在接受请求的同时就自动创建一个对象
OGNL:
对象图导航语言(Object graphic navigation language) 也是类似于JSTL用来访问值栈中的数据
例如:在index.jsp中定义:<S:property value ="name"/>输出一个value值
在LoginAction 中定义:

private String str;private User user;private List<USer> list;private Map<String,User> map;public void getMap(Map<String,User> map){return map;}public void setMap(Map<String,User> map){this.map=map;}public List<User> getList(List<User> list){return list;}pubic void setList(List<User> list){this.list=list;}public User setStr(String str){this.str=str;}public void getUser(){return user;} public User user()public void getStr(){return this.str;}public String execute(){this.str="aaaa";this.user=new User(111,"cccc");this.list=new ArrayList<List>();for(int i=0;i<3;i++){list.add(new User(i,"000"+i);this.map=new HashMap<String,User>();for(int i=0;i<3;i++){map.put("aa"+i,new User(i,"呵呵"+i));}return "success";

在index.jsp中定义

<body><h1>取普通属性</h1>通过标签取value的值:<S:property value="str"/>通过EL表达式取value的值:${str}<hr>取对象数据:<s:property value="user.name"/>${user.name}取list集合:<s:iterator value="list" var="u">    <s:property value="#u.name"/>    </s:iterator><s:debug/><hr></body>

用JSTL表达式表示:

引入<%@ taglib uri="http://java.sun.com/jsp/jst/core" prefix="c"%>body中添加:<c:forEach items="${list}" var="u">${u.name}

用Map表示:
在body中添加

<s:iterator value="map" var ="u"><s:property value="#u.value.name"/></s:iterator>
forEach表示方法:<c:forEach item="${map}" var="u">${u.value.id}</c:forEach>