struts2_3

02-ognl表达式介绍&准备工作_

03-ognl表达式语法_

04-ognl表达式语法02_

导包:不用导包,struts2包中已经有了,不需要导入额外的jar

EL取值环境

 

OGNL取值环境

 

package cn.itheima.a_ognl;

 

import java.util.HashMap;

import java.util.Map;

 

import org.junit.Test;

 

import cn.itheima.bean.User;

import ognl.Ognl;

import ognl.OgnlContext;

import ognl.OgnlException;

 

//展示OGNL语法

public class Demo {

@Test

//准备工作

public void fun1() throws Exception{

//准备ONGLContext

//准备Root

User rootUser = new User("tom",18);

//准备Context

Map<String,User> context = new HashMap<String,User>();

context.put("user1", new User("jack",18));

context.put("user2", new User("rose",22));

OgnlContext oc = new OgnlContext();

//将rootUser作为root部分

oc.setRoot(rootUser);

//将context这个Map作为Context部分

oc.setValues(context);

//书写OGNL

Ognl.getValue("", oc, oc.getRoot());

}

@Test

//基本语法演示

//取出root中的属性值

public void fun2() throws Exception{

//准备ONGLContext

//准备Root

User rootUser = new User("tom",18);

//准备Context

Map<String,User> context = new HashMap<String,User>();

context.put("user1", new User("jack",18));

context.put("user2", new User("rose",22));

OgnlContext oc = new OgnlContext();

oc.setRoot(rootUser);

oc.setValues(context);

//书写OGNL

//取出root中user对象的name属性

String name = (String) Ognl.getValue("name", oc, oc.getRoot());

Integer age = (Integer) Ognl.getValue("age", oc, oc.getRoot());

System.out.println(name);

System.out.println(age);

}

@Test

//基本语法演示

//取出context中的属性值

public void fun3() throws Exception{

//准备ONGLContext

//准备Root

User rootUser = new User("tom",18);

//准备Context

Map<String,User> context = new HashMap<String,User>();

context.put("user1", new User("jack",18));

context.put("user2", new User("rose",22));

OgnlContext oc = new OgnlContext();

oc.setRoot(rootUser);

oc.setValues(context);

//书写OGNL

//取出context中键为user1对象的name属性

String name = (String) Ognl.getValue("#user1.name", oc, oc.getRoot());

String name2 = (String) Ognl.getValue("#user2.name", oc, oc.getRoot());

Integer age = (Integer) Ognl.getValue("#user2.age", oc, oc.getRoot());

System.out.println(name);

System.out.println(name2);

System.out.println(age);

}

@Test

//基本语法演示

//为属性赋值

public void fun4() throws Exception{

//准备ONGLContext

//准备Root

User rootUser = new User("tom",18);

//准备Context

Map<String,User> context = new HashMap<String,User>();

context.put("user1", new User("jack",18));

context.put("user2", new User("rose",22));

OgnlContext oc = new OgnlContext();

oc.setRoot(rootUser);

oc.setValues(context);

//书写OGNL

//将root中的user对象的name属性赋值

Ognl.getValue("name='jerry'", oc, oc.getRoot());

String name = (String) Ognl.getValue("name", oc, oc.getRoot());

String name2 = (String) Ognl.getValue("#user1.name='郝强勇',#user1.name", oc, oc.getRoot());

System.out.println(name);

System.out.println(name2);

}

@Test

//基本语法演示

//调用方法

public void fun5() throws Exception{

//准备ONGLContext

//准备Root

User rootUser = new User("tom",18);

//准备Context

Map<String,User> context = new HashMap<String,User>();

context.put("user1", new User("jack",18));

context.put("user2", new User("rose",22));

OgnlContext oc = new OgnlContext();

oc.setRoot(rootUser);

oc.setValues(context);

//书写OGNL

//调用root中user对象的setName方法

Ognl.getValue("setName('lilei')", oc, oc.getRoot());

String name = (String) Ognl.getValue("getName()", oc, oc.getRoot());

String name2 = (String) Ognl.getValue("#user1.setName('lucy'),#user1.getName()", oc, oc.getRoot());

System.out.println(name);

System.out.println(name2);

}

@Test

//基本语法演示

//调用静态方法

public void fun6() throws Exception{

//准备ONGLContext

//准备Root

User rootUser = new User("tom",18);

//准备Context

Map<String,User> context = new HashMap<String,User>();

context.put("user1", new User("jack",18));

context.put("user2", new User("rose",22));

OgnlContext oc = new OgnlContext();

oc.setRoot(rootUser);

oc.setValues(context);

//书写OGNL

String name = (String) Ognl.getValue("@cn.itheima.a_ognl.HahaUtils@echo('hello 强勇!')", oc, oc.getRoot());

//Double pi = (Double) Ognl.getValue("@java.lang.Math@PI", oc, oc.getRoot());

Double pi = (Double) Ognl.getValue("@@PI", oc, oc.getRoot());

System.out.println(name);

System.out.println(pi);

}

@Test

//基本语法演示

//ognl创建对象-list|map

public void fun7() throws Exception{

//准备ONGLContext

//准备Root

User rootUser = new User("tom",18);

//准备Context

Map<String,User> context = new HashMap<String,User>();

context.put("user1", new User("jack",18));

context.put("user2", new User("rose",22));

OgnlContext oc = new OgnlContext();

oc.setRoot(rootUser);

oc.setValues(context);

//书写OGNL

//创建list对象

Integer size = (Integer) Ognl.getValue("{'tom','jerry','jack','rose'}.size()", oc, oc.getRoot());

String name = (String) Ognl.getValue("{'tom','jerry','jack','rose'}[0]", oc, oc.getRoot());

String name2 = (String) Ognl.getValue("{'tom','jerry','jack','rose'}.get(1)", oc, oc.getRoot());

/*System.out.println(size);

System.out.println(name);

System.out.println(name2);*/

//创建Map对象

Integer size2 = (Integer) Ognl.getValue("#{'name':'tom','age':18}.size()", oc, oc.getRoot());

String name3  = (String) Ognl.getValue("#{'name':'tom','age':18}['name']", oc, oc.getRoot());

Integer age  = (Integer) Ognl.getValue("#{'name':'tom','age':18}.get('age')", oc, oc.getRoot());

System.out.println(size2);

System.out.println(name3);

System.out.println(age);

}

 

}

 

 

Value Stack Contents

Object

Property Name

Property Value

cn.itheima.b_showvs.Demo1Action

container

There is no read method for container

errorMessages

[]

actionErrors

[]

actionMessages

[]

fieldErrors

{}

texts

null

locale

zh_CN

errors

{}

com.opensymphony.xwork2.DefaultTextProvider

texts

null

 

栈中默认放置的是访问的action对象

06-ognl表达式与Struts2框架结合体现-参数赋值_

 

package cn.itheima.c_param;

 

import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionSupport;

import com.opensymphony.xwork2.ModelDriven;

import com.opensymphony.xwork2.Preparable;

import com.opensymphony.xwork2.util.ValueStack;

 

import cn.itheima.bean.User;

 

public class Demo2Action extends ActionSupport implements ModelDriven<User> {

private User u = new User();

@Override

public String execute() throws Exception {

System.out.println(u);

return SUCCESS;

}

 

/*@Override

public void prepare() throws Exception {

//压入栈顶

//1获得值栈

ValueStack vs = ActionContext.getContext().getValueStack();

ValueStack vs = ActionContext.getContext().getValueStack();

//2u压入栈顶

vs.push(u);

}*/

 

@Override

public User getModel() {

return u;

}

 

}

 

07-ognl表达式与Struts2框架结合体现-配置文件中使用ognl_

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

<package name="showvs" namespace="/" extends="struts-default" >

<action name="Demo1Action" class="cn.itheima.b_showvs.Demo1Action" method="execute" >

<result name="success" type="dispatcher" >/showvs.jsp</result>

</action>

<action name="Demo2Action" class="cn.itheima.c_param.Demo2Action" method="execute" >

<result name="success" type="dispatcher" >/form.jsp</result>

</action>

<action name="Demo3Action" class="cn.itheima.d_config.Demo3Action" method="execute" >

<result name="success" type="redirectAction" >

<param name="actionName">Demo1Action</param>

<param name="namespace">/</param>

<!-- 如果添加的参数struts"看不懂".就会作为参数附加重定向的路径之后.

 如果参数是动态的.可以使用${}包裹ognl表达式.动态取值

 -->

<param name="name">${name}</param>

</result>

</action>

</package>

</struts>

package cn.itheima.d_config;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class Demo3Action extends ActionSupport {

private String  name;

@Override

public String execute() throws Exception {

name = "zhangtingting";//从数据库中查询

return SUCCESS;

}

 

public String getName() {

return name;

}

 

public void setName(String name) {

this.name = name;

}

 

 

}

 

 

Demo3Action重定向到Demo1Action,如果想带参数过去,可以在重定向的配置中:

<!-- 如果添加的参数struts"看不懂".就会作为参数附加重定向的路径之后.

 如果参数是动态的.可以使用${}包裹ognl表达式.动态取值

 -->

这个时候访问Demo3Action

http://localhost:8080/struts2_day03/Demo3Action

重定向之后路径变为:

http://localhost:8080/struts2_day03/Demo1Action.action?name=zhangtingting

08-扩展内容-struts2流程源码_

 

结合架构图看源码;

StrutsPrepareAndExecuteFilter经过此过滤器

过滤器调用doFilter()方法

doFilter(ServletRequest req, ServletResponse res, FilterChain chain)

if (excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {

                chain.doFilter(request, response);

            }

判读请求是否由struts2处理,如果不是则方形,如是,则进入下面代码:

    prepare.createActionContext(request, response);

创建数据中心,具体而言是:

ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();

            stack.getContext().putAll(dispatcher.createContextMap(request, response, null));

            ctx = new ActionContext(stack.getContext());

先获得值栈,再创建ctx 数据中心

 request = prepare.wrapRequest(request);

struts包装的request对象

request.getAttribute()方法,

  ActionMapping mapping = prepare.findActionMapping(request, response, true);

String namespace = mapping.getNamespace();

String name = mapping.getName();

String method = mapping.getMethod();

ActionProxy proxy = getContainer().getInstance(ActionProxyFactory.class).createActionProxy(

                    namespace, name, method, extraContext, true, false);

 

            request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());

 

            // if the ActionMapping says to go straight to a result, do it!

            if (mapping.getResult() != null) {

                Result result = mapping.getResult();

                result.execute(proxy.getInvocation());

            } else {

                proxy.execute();

            }

下面是调用拦截器的interceptor()方法

调用action的某个方法,返回某个字符串,根据字符串的值,去调用对应的result,由result去转发到某页面。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值