您的位置:首页 > 编程语言 > Java开发

Struts2_07_值栈与 OGNL

2018-03-02 17:02 330 查看

1、值栈简介

        值栈(ValueStack)是对应每个请求对象的一套内存数据的封装,Struts2 会给每个请求创建一个新的值栈。
值栈能够线程安全地为每个请求提供公共的数据存取服务。
在ValueStack对象内部有两个逻辑部分。
      ObjectStack:  root属性,是一个ArrayList,包含Action对象和其他对象
      ContextMap:  context属性,是一个Map,默认压入内容(request、session、application、attr、parameters)。



下面是一个通过简单的例子实现值栈获取action属性值,在action里把值直接压入值栈,在jsp中取出来,这是在ObjectStack上进行的操作。
actionpackage com.java1234.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);

return SUCCESS;
}

}

jsp,引入了struts标签库,使用<property>标签获取值栈里的值<%@ 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");
%>
</head>
<body>
获取狭义上的值栈数据:<s:property value="name"/>
<s:property value="age"/><br/>

</body>
</html>运行结果



2、OGNL引入

GNL 是对象图导航语言 Object-Graph Navigation Language 的缩写,它是一种功能强大的表达式语言。
OGNL 访问 ValueStack 数据
<s:property value=”account” />
OGNL 访问 ActionContext 数据
访问某个范围下的数据要用#
#parameters 请求参数 request.getParameter(...);
#request 请求作用域中的数据 request.getAttribute(...);
#session 会话作用域中的数据 session.getAttribute(...);
#application 应用程序作用域中的数据 application.getAttribute(...);
#attr 按照 page request session application 顺序查找值,相当于EL里的${}
加了#号的都是在值栈的ContextMap取值
实例代码如下
actionpackage com.java1234.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;
}

}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");
%>
</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>
运行结果



3、OGNL 访问复杂对象

访问action里的 对象,list,map,方法如下先准备一个beanpackage com.java1234.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;
}

}

actionpackage com.java1234.action;

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

import com.java1234.model.Student;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport{

/**
*
*/
private static final long serialVersionUID = 1L;

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 {
ActionContext actionContext=ActionContext.getContext();
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;
}

}

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>

</head>
<body>
<!--以下几种就是OGNL访问访问复杂类型的方式-->
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"/><br/>
<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"/><br/>
<s:property value="studentMap['badStudent'].name"/>
<s:property value="studentMap['badStudent'].age"/><br/>
</body>
</html>运行结果是



4、OGNL 访问静态方法和属性

准备一个类
package com.java1234.common;

public class MyStatic {

public static final String str="访问了静态属性";

public static String printUrl(){
System.out.println("访问了静态方法");
return "http://www.访问了静态方法";
}
}

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>
</head>
<body>
<!-- ognl通过@来访问类,静态属性和静态方法 -->
访问静态属性: <s:property value="@com.java1234.common.MyStatic@str"/><br/>
访问静态方法:<s:property value="@com.java1234.common.MyStatic@printUrl()"/>
</body>
</html>在struts.xml上添加<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<!-- 添加下面这句,设置允许访问-->
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
<package name="hello" namespace="/" extends="struts-default">
<action name="hello" class="com.java1234.action.HelloAction">
<result name="success" >success.jsp</result>
</action>
</package>
</struts>运行结果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Struts2 ongl