一、结合图示
二、结合说明
OGNL表达式想要运行,需要一个OGNLContext。Struts2准备了一个OGNLContext,叫做 ValueStack(值栈)。根据我们知道了解的,OGNL由三个部分组成:表达式、Root、context,然而表达式是我们查询的时候使用的,由此我们可以知道,ValueStack由两部分组成。
ValueStack的两部分是:一部分是Root,放置的是一个栈。另外一部分context,是将ActionContext放入。
首先我们看到 ValueStack,这里面它使用的其实是它的实现类OgnlValueStack
public class OgnlValueStack implements Serializable, ValueStack, ClearableValueStack, MemberAccessValueStack {
.......
protected CompoundRoot root;
protected transient Map<String, Object> context;
......
}
可以看到,ValueStack 准备的 root 的类型是CompounRoot。 看到CompounRoot
/**
* A Stack that is implemented using a List.
*
* @author plightbo
* @version $Revision$
*/
public class CompoundRoot extends CopyOnWriteArrayList<Object> {
private static final long serialVersionUID = 8563229069192473995L;
public CompoundRoot() {
}
public CompoundRoot(List<?> list) {
super(list);
}
public CompoundRoot cutStack(int index) {
return new CompoundRoot(subList(index, size()));
}
public Object peek() {
return get(0);
}
/**
* 出栈
*/
public Object pop() {
return remove(0);
}
/**
* 入栈
*/
public void push(Object o) {
add(0, o);
}
}
CopyOnWriteArrayList相当于线程安全的 ArrayList。所以这里面栈其实是使用CopyOnWriteArrayList来实现的。入栈就是在集合的第0个位置中添加对象,出栈就是移除第0个位置的对象,这样便实现了栈的功能(先进先出)。这样我们查找我们需要的值的时候,就会从栈顶查找,找不到就会向下查找,找到就停止。
三、查看值栈中的内容
既然现在已经知道Struts2会给我们提供一个值栈,那么就来看看值栈中的内容吧。
3.1 StackDemoAction
package com.qwm.struts2_3.b_stack;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author: wiming
* @date: 2017-09-20 15:15:28 星期三
* @decription:
* 查看值栈中的值
*/
public class StackDemoAction extends ActionSupport{
@Override
public String execute() throws Exception {
System.out.println("--查看值栈--");
return SUCCESS;
}
}
3.2 struts.xml 配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="stack" namespace="/" extends="struts-default">
<action name="stackdemoAction" class="com.qwm.struts2_3.b_stack.StackDemoAction">
<result name="success">message.jsp</result>
</action>
</package>
</struts>
3.3 message.jsp
<%--
Created by IntelliJ IDEA.
User: qiwenming
Date: 2017/9/20
Time: 15:19
To change this template use File | Settings | File Templates.
--%>'
<%--导入struts提供的标签库--%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>值栈信息查看</title>
</head>
<body>
<!-- 调试标签 -->
<s:debug></s:debug>
</body>
</html>
3.4 Root部分
默认情况下,栈中放置当前访问的Action对象
3.4 Context部分
Context部分其实就是ActionContext
request
response
ServletContext
requestScope
sessionScope
applicationScope
params
attrs
…