struts2学习值栈简介与OGNL引入

Posted 有点懒惰的大青年

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了struts2学习值栈简介与OGNL引入相关的知识,希望对你有一定的参考价值。

一、值栈简介:

二、OGNL引入:

com.cy.action.HelloAction.java:

package com.cy.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{
    private static final long serialVersionUID = 1L;

    @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:

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

success.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<%
    request.setAttribute("name", "李四(request)");
    request.setAttribute("age", 12);
    
    pageContext.setAttribute("name", "小沛(page)");
    pageContext.setAttribute("age", "18");
%>
</head>
<body>
    获取狭义上的值栈数据:<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>
</body>
</html>

测试结果:

 

 三、OGNL访问复杂对象:

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

com.cy.model.Student.java:

package com.cy.model;

public class Student {
    private String name;
    private int age;
    
    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    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;
    }
}
View Code

com.cy.common.MyStatic.java:静态属性和静态方法:

package com.cy.common;

public class MyStatic {
    public static final String str = "好好学习";
    
    public static String print(){
        return "天天向上";
    }
}
View Code

com.cy.action.HelloAction.java中存入javaBean、List、Map:

package com.cy.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cy.model.Student;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;

public class HelloAction extends ActionSupport{
    private static final long serialVersionUID = 1L;
    
    private Student student;
    private List<Student> students;
    private Map<String,Student> studentMap;
    
    public Map<String, Student> getStudentMap() {
        return studentMap;
    }

    public void setStudentMap(Map<String, Student> studentMap) {
        this.studentMap = studentMap;
    }

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    @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);
        
        student = new Student("小八", 15);
        
        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;
    }
    
}
View Code

struts.xml配置,开启ognl允许访问静态方法:

<struts>
    <!-- 允许OGNL访问静态方法 -->
    <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
    
    <package name="manage" namespace="/" extends="struts-default">
        <action name="hello" class="com.cy.action.HelloAction">
            <result name="success">success.jsp</result>
        </action>
                    
    </package>
    
</struts>

success.jsp,通过ognl来取值:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>    
    ognl访问javaBean对象:<s:property value="student.name"/>
    <s:property value="student.age"/><br>
    ognl访问List集合:<s:property value="students[0].name"/>
    <s:property value="students[0].age"/>
    <s:property value="students[1].name"/>
    <s:property value="students[1].age"/><br/>
    ognl访问Map:<s:property value="studentMap[\'goodStudent\'].name"/>
    <s:property value="studentMap[\'goodStudent\'].age"/>
    <s:property value="studentMap[\'badStudent\'].name"/>
    <s:property value="studentMap[\'badStudent\'].age"/><br/>
    
    ognl访问静态属性:<s:property value="@com.cy.common.MyStatic@str"/><br>
    <!-- 访问静态方法,有些封装好的Util工具,转换等,就可以直接调用了 -->
    ognl访问静态方法:<s:property value="@com.cy.common.MyStatic@print()"/>
</body>
</html>

测试:

 

 

 

---------

以上是关于struts2学习值栈简介与OGNL引入的主要内容,如果未能解决你的问题,请参考以下文章

[ SSH框架 ] Struts2框架学习之三(OGNl和ValueStack值栈学习)

Struts2 进阶

Struts2学习———— ognl表达式值栈actionContext之间的关系

Struts2学习第七课 OGNL

struts2值栈分析

Struts2学习记录-Value Stack(值栈)和OGNL表达式