Struts2学习(八)【OGNL表达式与Struts框架结合之原理】

一、结合图示

这里写图片描述


二、结合说明

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值