struts2值栈与OGNL

阅读更多
1. struts2值栈简介

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

新建项目HeadFirstStruts2Chap04


2. struts2引入OGNL

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

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


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 session = actionContext.getSession();
        session.put("name", "王五(session)");
        session.put("age", 13);
        Map application = actionContext.getApplication();
        application.put("name", "赵六(application)");
        application.put("age", 14);
        return SUCCESS;
    }
}

struts.xml


      
          success.jsp
      


success.jsp

<%@taglib prefix="s" uri="/struts-tags" %>
<%
    request.setAttribute("name", "李四(request)");
    request.setAttribute("age", "12");
%>
获取狭义上的值栈数据:

请求参数:
request:
session:
application:
attr取值:
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 students;
    private Map studentMap;
    public Student getStudent() {
        return student;
    }
    public void setStudent(Student student) {
        this.student = student;
    }
    public List getStudents() {
        return students;
    }
    public void setStudents(List students) {
        this.students = students;
    }
    public Map getStudentMap() {
        return studentMap;
    }
    public void setStudentMap(Map studentMap) {
        this.studentMap = studentMap;
    }
    @Override
    public String execute() throws Exception {
        student = new Student("小三", 12);
        students = new ArrayList();
        students.add(new Student("小四", 13));
        students.add(new Student("小五", 14));
        studentMap = new HashMap();
        studentMap.put("goodStudent", new Student("学霸", 20));
        studentMap.put("badStudent", new Student("学渣", 19));
        return SUCCESS;
    }
}

struts.xml


      
          student.jsp
      


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


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

1. 访问静态属性;
2. 访问静态方法;



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



ognl_static.jsp

<%@taglib prefix="s" uri="/struts-tags" %>
访问静态属性:
访问静态方法: http://localhost:8080/HeadFirstStruts2Chap04/ognl_static.jsp 访问静态属性:百度 访问静态方法:http://www.baidu.com

你可能感兴趣的:(Java,struts2)