(四)值栈与OGNL

所有的学习我们必须先搭建好Struts2的环境(1、导入对应的jar包,2、web.xml,3、struts.xml)

第一节:值栈简介

值栈是对应每个请求对象的一套内存数据的封装,Struts2 会给每个请求创建一个新的值栈。

值栈能够线程安全地为每个请求提供公共的数据存取服务。

第二节:OGNL 引入

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

OGNL 访问ValueStack 数据

  <s:property value=”account” />

  注:这里要添加<%@taglib prefix="s" uri="/struts-tags" %>struts的标签库

OGNL 访问ActionContext 数据

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

    #parameters 请求参数request.getParameter(...);

    #request 请求作用域中的数据request.getAttribute(...);

    #session 会话作用域中的数据session.getAttribute(...);

    #application 应用程序作用域中的数据application.getAttribute(...);

    #attr 按照page request session application 顺序查找值

例子:

 struts.xml
1
<?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 8 <package name="manage" namespace="/" extends="struts-default"> 9 10 <action name="hello" class="com.wishwzp.action.HelloAction"> 11 <result name="success" >success.jsp</result> 12 </action> 13 </package> 14 15 </struts>

 

 HelloAction.java
1
package com.wishwzp.action; 2 3 import java.util.Map; 4 5 import com.opensymphony.xwork2.ActionContext; 6 import com.opensymphony.xwork2.ActionSupport; 7 import com.opensymphony.xwork2.util.ValueStack; 8 9 public class HelloAction extends ActionSupport{ 10 11 /** 12 * 13 */ 14 private static final long serialVersionUID = 1L; 15 16 17 18 @Override 19 public String execute() throws Exception { 20 // 获取ActionContext 21 ActionContext actionContext=ActionContext.getContext(); 22 // 获取狭义上的值栈 23 // 值栈是用来存储数据的 24 ValueStack valueStack=actionContext.getValueStack(); 25 valueStack.set("name", "张三(valueStack)"); 26 valueStack.set("age", 11); 27 28 Map<String, Object> session=actionContext.getSession(); 29 session.put("name", "王五(session)"); 30 session.put("age", 13); 31 32 Map<String, Object> application=actionContext.getApplication(); 33 application.put("name", "赵六(application)"); 34 application.put("age", 14); 35 return SUCCESS; 36 } 37 }

 

 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 <% 10 request.setAttribute("name", "李四(request)"); 11 request.setAttribute("age", "12"); 12 %> 13 </head> 14 <body> 15 获取狭义上的值栈数据:<s:property value="name"/> 16 <s:property value="age"/><br/> 17 请求参数:<s:property value="#parameters.name"/> 18 <s:property value="#parameters.age"/><br/> 19 request:<s:property value="#request.name"/> 20 <s:property value="#request.age"/><br/> 21 session:<s:property value="#session.name"/> 22 <s:property value="#session.age"/><br/> 23 application:<s:property value="#application.name"/> 24 <s:property value="#application.age"/><br/> 25 attr取值:<s:property value="#attr.name"/> 26 <s:property value="#attr.age"/><br/> 27 </body> 28 </html>

 

url:http://localhost:8080/Struts2Chap04/hello?name=ssss&age=23

结果:

第三节:OGNL 访问复杂对象

1,访问javaBean 对象;

2,访问集合对象;

3,访问Map 对象;

例子:

 struts.xml
1
<?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 8 <package name="manage" namespace="/" extends="struts-default"> 9 10 <action name="hello" class="com.wishwzp.action.HelloAction"> 11 <result name="success" >success.jsp</result> 12 </action> 13 </package> 14 15 </struts>

 

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

 

 HelloAction.java
1
package com.wishwzp.action; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 8 import com.wishwzp.model.Student; 9 import com.opensymphony.xwork2.ActionContext; 10 import com.opensymphony.xwork2.ActionSupport; 11 import com.opensymphony.xwork2.util.ValueStack; 12 13 public class HelloAction extends ActionSupport{ 14 15 /** 16 * 17 */ 18 private static final long serialVersionUID = 1L; 19 20 //访问javaBean对象,并get...和set... 21 private Student student; 22 23 //访问集合对象,并get...和set... 24 private List<Student> students; 25 26 //访问Map对象,并get...和set... 27 private Map<String,Student> studentMap; 28 29 public Map<String, Student> getStudentMap() { 30 return studentMap; 31 } 32 33 public void setStudentMap(Map<String, Student> studentMap) { 34 this.studentMap = studentMap; 35 } 36 37 public List<Student> getStudents() { 38 return students; 39 } 40 41 public void setStudents(List<Student> students) { 42 this.students = students; 43 } 44 45 public Student getStudent() { 46 return student; 47 } 48 49 public void setStudent(Student student) { 50 this.student = student; 51 } 52 53 @Override 54 public String execute() throws Exception { 55 56 //javaBean对象 57 student=new Student("小扒", 12); 58 59 //集合对象 60 students=new ArrayList<Student>(); 61 students.add(new Student("老九",13)); 62 students.add(new Student("老十",14)); 63 64 //Map对象 65 studentMap=new HashMap<String,Student>(); 66 studentMap.put("goodStudent", new Student("学霸",20)); 67 studentMap.put("badStudent", new Student("学渣",19)); 68 return SUCCESS; 69 } 70 71 }

 

 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 <body> 11 ognl访问javaBean对象:<s:property value="student.name"/> 12 <s:property value="student.age"/><br/> 13 14 ognl访问List集合:<s:property value="students[0].name"/> 15 <s:property value="students[0].age"/><br/> 16 <s:property value="students[1].name"/> 17 <s:property value="students[1].age"/><br/> 18 19 ognl访问Map:<s:property value="studentMap['goodStudent'].name"/> 20 <s:property value="studentMap['goodStudent'].age"/><br/> 21 <s:property value="studentMap['badStudent'].name"/> 22 <s:property value="studentMap['badStudent'].age"/><br/> 23 </body> 24 </html>

url访问:http://localhost:8080/Struts2Chap04/hello

结果:

第四节:OGNL 访问静态方法和属性

1,访问静态属性;

2,访问静态方法;需要在struts.xml加上<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>

例子:

 

 struts.xml
1
<?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 8 <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant> 9 10 <package name="manage" namespace="/" extends="struts-default"> 11 12 </package> 13 14 </struts>

 

 MyStatic.java
1
package com.wishwzp.common; 2 3 public class MyStatic { 4 5 //静态属性 6 public static final String str="Struts2开心学习"; 7 8 //静态方法 9 public static String printUrl(){ 10 System.out.println("http://www.baidu.com"); 11 return "http://www.baidu.com"; 12 } 13 }

 

 ognl_static.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 <body> 11 访问静态属性: <s:property value="@com.wishwzp.common.MyStatic@str"/><br/> 12 访问静态方法:<s:property value="@com.wishwzp.common.MyStatic@printUrl()"/> 13 </body> 14 </html>

 

url:http://localhost:8080/Struts2Chap04/ognl_static.jsp

结果:

 

posted @ 2016-05-08 17:58  kika  阅读(252)  评论(0编辑  收藏  举报