Struts2的值栈和OGNL牛逼啊

Struts2的值栈和OGNL牛逼啊

值栈简介:

值栈是对应每个请求对象的一套内存数据的封装,Struts2会给每个请求创建一个新的值栈,值栈能够线程安全的为每个请求提供公共的数据存取服务。

OGNL介绍:

1)基本数据:

OGNL 是对象图导航语言 Object-GraphNavigationLanguage 的缩写,它是一种功能强大的表达式语言。

OGNL 访问 ValueStack 数据 <s:propertyvalue=”account”/>

OGNL 访问 ActionContext 数据

访问某个范围下的数据要用

#parameters 请求参数 request.getParameter(...); 
#request 请求作用域中的数据 request.getAttribute(...); 
#session 会话作用域中的数据 session.getAttribute(...); 
#application 应用程序作用域中的数据 application.getAttribute(...); 
#attr 按照 page   request     session       application 顺序查找值

我们以例子理解这部分内容,设置HelloAction

 1 public class HelloAction extends ActionSupport{
 2     private static final long serialVersionUID = 1L;
 3     @Override
 4     public String execute() throws Exception {
 5         //狭义上的值栈
 6         ActionContext actionContext=ActionContext.getContext();
 7         ValueStack valueStack=actionContext.getValueStack();
 8         valueStack.set("name", "张三(ValueStack)");
 9         valueStack.set("age", 11);
10         //session中的值
11         Map<String, Object> session=actionContext.getSession();
12         session.put("name","王五(session)");
13         session.put("age","13");
14         //application中的内容
15         Map<String, Object> application=actionContext.getApplication();
16         application.put("name", "赵六(application)");
17         application.put("age","14");
18         return SUCCESS;
19     }
20 }

Struts.xml文件的配置:

1 <struts>
2    <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant> 
3   <package name="manage" namespace="/" extends="struts-default">
4       <action name="hello" class="com.java1234.Action.HelloAction">
5           <result name="success" >success.jsp</result>
6       </action>
7   </package>
8 </struts>

前端页面success.jsp

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3     <%@taglib prefix="s" uri="/struts-tags" %>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 8 <title>Insert title here</title>
 9 </head>
10 <%
11     request.setAttribute("name", "李四(request)");
12     request.setAttribute("age", "12");
13 %>
14 <body>
15     值栈 获取的数据:<s:property value="name"/>
16                 <s:property value="age"/>
17     <br/>
18     前台传递的数据:<s:property value="#parameters.name"/>
19                 <s:property value="#parameters.age"/>
20     <br/>
21     request中的数据:<s:property value="#request.name"/>
22                   <s:property value="#request.age"/>
23     <br/>
24     session中的数据:<s:property value="#session.name"/>
25                   <s:property value="#session.age"/> 
26                   <br/>
27 application的数据: <s:property value="#application.name"/>
28                   <s:property value="#application.age"/> 
29                   <br/>
30     attr取值  :   <s:property  value="#attr.name"/>
31                  <s:property  value="#attr.age"/>
32                  <br/>
33 </body>
34 </html>

首先,是取值方式<s:property    value="方式"/>

①值栈 直接取 比如说是name age   就可以使用这种方式  value=”name”  value=”age”

page页面传递的数据 比如说是name age  使用这种方式  value="#parameters.name”  value="#parameters.age”

requset 设置的值 使用的方式  value="#request.name"    value="#request.age"

session设置的值使用的方式  value="#session.name"      value="#session.age"

application设置的值使用的方式 value="#application.name"   value="#application.age"

之后attr的取值方式是按照 page request session applicaiton这个顺序取得。

例如:attr获取的是request的值

 

2OGNL 访问静态方法和属性

Mystatic类:

 1 public class MyStatic {
 2 
 3 public static final String str="yxs";
 4 
 5 public static String printUrl(){
 6 
 7 System.out.println("http://www.yxs.com");
 8 
 9 return "http://www.yxs.com";
10 
11 }
12 
13 }

 

Static.jsp直接访问:

 

1 <body>
2 访问静态属性: <s:property value="@com.java1234.common.MyStatic@str"/><br/>
3 访问静态方法:<s:property value="@com.java1234.common.MyStatic@printUrl()"/>
4 </body>

结果:

(3)OGNL 访问复杂对象

 

我们以javaBean对象为例:Student

 

 1 public class Student {
 2     private String name;
 3     private int age;
 4     public Student() {
 5         super();
 6         // TODO Auto-generated constructor stub
 7     }
 8     public Student(String name, int age) {
 9         super();
10         this.name = name;
11         this.age = age;
12     }
13     public String getName() {
14         return name;
15     }
16     public void setName(String name) {
17         this.name = name;
18     }
19     public int getAge() {
20         return age;
21     }
22     public void setAge(int age) {
23         this.age = age;
24     }
25 }

 

Success.jsp文件:

 

 

 1 <html>
 2 <body>
 3 ognl的javaBean值: <s:property  value="student.name"/>
 4                       <s:property  value="student.age"/>
 5                  <br/>
 6     ognl的List集合: <s:property  value="students[0].name"/>
 7                   <s:property  value="students[0].age"/>
 8                  <br/>
 9                   <s:property  value="students[1].name"/>
10                  <s:property  value="students[1].age"/>
11                  <br/>
12                  <s:property  value="students[2].name"/>
13                  <s:property  value="students[2].age"/>
14                  <br/>
15     ognl的Map: <s:property  value="studentMap['goodStudent'].name"/>
16                   <s:property  value="studentMap['goodStudent'].age"/>
17                  <br/>
18                   <s:property  value="studentMap['badStudent'].name"/>
19                  <s:property  value="studentMap['badStudent'].age"/>
20                  <br/>
21 </body>
22 </html>

HelloAction文件代码:

 

 1 public class HelloAction extends ActionSupport{
 2     private static final long serialVersionUID = 1L;
 3     private Student student;//javaBean
 4     private List<Student>students;//list
 5     private Map<String,Student>studentMap;//Map
 6     public Student getStudent() {
 7         return student;
 8     }
 9 
10     public void setStudent(Student student) {
11         this.student = student;
12     }
13     
14     public List<Student> getStudents() {
15         return students;
16     }
17 
18     public void setStudents(List<Student> students) {
19         this.students = students;
20     }
21     
22     public Map<String, Student> getStudentMap() {
23         return studentMap;
24     }
25 
26     public void setStudentMap(Map<String, Student> studentMap) {
27         this.studentMap = studentMap;
28     }
29 
30     @Override
31     public String execute() throws Exception {
32         // TODO Auto-generated method stub
33         
34         students=new ArrayList<Student>();
35         student=new Student("小八",12);
36         students.add(new Student("小酒",13));
37         students.add(new Student("小石",14));
38         students.add(new Student("十一",15));
39         studentMap=new HashMap<String,Student>();
40         studentMap.put("goodStudent", new Student("学霸",20));
41         studentMap.put("badStudent", new Student("学渣",19));
42         return SUCCESS;
43     }
44 }

显示结果:

转载于:https://www.cnblogs.com/zyxsblogs/p/10925146.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值