欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

struts2值栈与OGNL

程序员文章站 2022-07-12 16:14:06
...
1. struts2值栈简介

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

新建项目HeadFirstStruts2Chap04


2. struts2引入OGNL

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

OGNL访问ValueStack数据
<%@taglib prefix="s" uri="/struts-tags" %>
<s:property value="account"/>

OGNL访问ActionContext数据
访问某个范围下的数据要用#
#parameters.xxx 请求参数 request.getParameter(xxx);
#request.xxx 请求作用域中的数据 request.getAttribute(xxx);
#session.xxx 会话作用域中的数据 session.getAttribute(xxx);
#application.xxx 应用程序作用域中的数据 application.getAttribute(xxx);
#attr.xxx 按照page request session application顺序查找值


1) OGNL访问数据

HelloAction.java

package com.andrew.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;
public class HelloAction extends ActionSupport {
    @Override
    public String execute() throws Exception {
        ActionContext actionContext = ActionContext.getContext();
        // 获取狭义上的值栈
        ValueStack valueStack = actionContext.getValueStack();
        valueStack.set("name", "张三(valueStack)");
        valueStack.set("age", 11);
        Map<String, Object> session = actionContext.getSession();
        session.put("name", "王五(session)");
        session.put("age", 13);
        Map<String, Object> application = actionContext.getApplication();
        application.put("name", "赵六(application)");
        application.put("age", 14);
        return SUCCESS;
    }
}

struts.xml

<package name="manage" namespace="/" extends="struts-default">
      <action name="hello" class="com.andrew.action.HelloAction">
          <result name="success" >success.jsp</result>
      </action>
</package>

success.jsp

<%@taglib prefix="s" uri="/struts-tags" %>
<%
    request.setAttribute("name", "李四(request)");
    request.setAttribute("age", "12");
%>
获取狭义上的值栈数据:<s:property value="name"/>
<s:property value="age"/><br/>
请求参数:<s:property value="#parameters.name"/>
<s:property value="#parameters.age"/><br/>
request:<s:property value="#request.name"/>
<s:property value="#request.age"/><br/>
session:<s:property value="#session.name"/>
<s:property value="#session.age"/><br/>
application:<s:property value="#application.name"/>
<s:property value="#application.age"/><br/>
attr取值:<s:property value="#attr.name"/>
<s:property value="#attr.age"/><br/>

http://localhost:8080/HeadFirstStruts2Chap04/hello
获取狭义上的值栈数据:张三(valueStack) 11
请求参数: 
request:李四(request) 12
session:王五(session) 13
application:赵六(application) 14
attr取值:李四(request) 12


2) OGNL访问复杂对象

1. 访问javaBean对象;
2. 访问集合对象;
3. 访问Map对象;

Student.java

package com.andrew.model;
public class Student {
    private String name;
    private int age;
    public Student() {}
    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

StudentAction.java

package com.andrew.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.andrew.model.Student;
import com.opensymphony.xwork2.ActionSupport;
public class StudentAction extends ActionSupport {
    private Student student;
    private List<Student> students;
    private Map<String, Student> studentMap;
    public Student getStudent() {
        return student;
    }
    public void setStudent(Student student) {
        this.student = student;
    }
    public List<Student> getStudents() {
        return students;
    }
    public void setStudents(List<Student> students) {
        this.students = students;
    }
    public Map<String, Student> getStudentMap() {
        return studentMap;
    }
    public void setStudentMap(Map<String, Student> studentMap) {
        this.studentMap = studentMap;
    }
    @Override
    public String execute() throws Exception {
        student = new Student("小三", 12);
        students = new ArrayList<Student>();
        students.add(new Student("小四", 13));
        students.add(new Student("小五", 14));
        studentMap = new HashMap<String, Student>();
        studentMap.put("goodStudent", new Student("学霸", 20));
        studentMap.put("badStudent", new Student("学渣", 19));
        return SUCCESS;
    }
}

struts.xml

<package name="manage" namespace="/" extends="struts-default">
      <action name="student" class="com.andrew.action.StudentAction">
          <result name="success" >student.jsp</result>
      </action>
</package>

http://localhost:8080/HeadFirstStruts2Chap04/student
ognl访问javaBean对象:小三 12
ognl访问List集合:小四 13
小五 14
ognl访问Map:学霸 20
学渣 19


3) OGNL访问静态方法和属性

1. 访问静态属性;
2. 访问静态方法;
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>


MyStatic.java

package com.andrew.common;
public class MyStatic {
    public static final String str = "百度";
    public static String printUrl(){
        System.out.println("http://www.baidu.com");
        return "http://www.baidu.com";
    }
}

struts.xml

<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>

ognl_static.jsp

<%@taglib prefix="s" uri="/struts-tags" %>
访问静态属性:<s:property value="@com.andrew.common.MyStatic@str"/><br/>
访问静态方法:<s:property value="@com.andrew.common.MyStatic@printUrl()"/>

http://localhost:8080/HeadFirstStruts2Chap04/ognl_static.jsp
访问静态属性:百度
访问静态方法:http://www.baidu.com 
相关标签: Java struts2