Struts2 的 OGNL

本文深入探讨了OGNL(对象图导航语言)的基本概念、语法及应用,特别是其与Struts2框架的结合原理。OGNL作为一种强大的表达式语言,能够灵活地操作Java对象,而Struts2则利用OGNL进行参数解析、对象创建和属性赋值。文章详细解释了值栈(ValueStack)的作用,以及如何在ActionContext中存储和检索数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#OGNL
##OGNL 简介
OGNL 的全称是对象图导航语言( Object-Graph Navigation Language),它是一种功能强大的开源表达式语言,使用这种表达式语言,可以通过某种表达式语法,存储 java 对象的任意属性,调用 java 对象的方法,同时能够自动实现必要的类型转换。如果把表达式看作是一种带有语义的字符串,那么 OGNL 无疑成为了这个语义字符串于 java 对象之间的沟通桥梁。
##OGNL 的要素

  • 表达式

这里写图片描述

  • 根对象 (Root)

这里写图片描述

  • Context 对象

这里写图片描述

大概结构图如下:
这里写图片描述
##OGNL 语法
基本取值

	//取出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);
	
     //---------------------------------------------------
    	//取出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);
	}	
	

创建对象(List、Map)

	@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);
	}	

#OGNL 与 Struts2 的结合
##值栈
ValueStack 是 Struts 的一个接口,字面意义为值栈,OgnlvalueStack 是 ValueStack 的实现类,客户端发起一个请求,Struts2 架构会创建一个 action 实例同时创建一个 OnglvalueStack 实例,OnglvalueStack 贯彻整个 Action 的生命周期,struts2 中使用 OGNL 将请求 Action 的参数封装为对象存储到值栈中,并通过 OGNL 表达式读取中的对象属性
##结合原理
OgnlvalueStack 中包括两部分,值栈和 map(即 ognl 上下文)

这里写图片描述

Context:即 OgnlContext 上下文,它是一个 map 结构,也就是我们之前学的 ActionContext (数据中心)。

CompounRoot:继承了 ArrayList,实现了压栈和出栈的功能,作为上下文的 Root 对象,存储了 Action 实例。

另外,查看值栈中两部分的内容可以使用 <s:debug></s:debug> 标签

PS:有时候,人们也叫 Root 部分为值栈(ValueStack)
##OgnlValueStack 的数据存取

我们发送请求时,核心控制器会创建 ActionContext,里面包含了 ValueStack。
###StackContext 中存取数据

(一)向StackContext中存放数据,ActionContext是一个Map 

        第一步:创建一个Action类 
        public class SaveDemo extends ActionSupport {
              public String execute(){
                    //获取ActionContext
                    ActionContext context=ActionContext.getContext();
                    //存放数据
                    context.put("hello", "hello context");
                    return SUCCESS;
                }
         } 
        第二步:配置Action 

                    <action name="save" class="com.cad.struts2.action.SaveDemo">
                        <result>/Demo.jsp</result>
                    </action> 
        第三步:查看ActionContext里的内容,使用<s:debug />标签 

        会发现有一行 
        hello   hello context 
        说明已经存入。

这里写图片描述

(二)向StackContext中的Session,application,attr等里面存放数据
                我们前面说过ActionContext是一个Map,里面包含的request,session等也都是Map。

                    第一步:创建Action类
                    public class SaveDemo extends ActionSupport {
                        public String execute(){
                            //第一种获取ActionContext中的session,是一个Map集合 
                            Map<String,Object> session=ActionContext.getContext().getSession();
                            session.put("hello", "hello session");  
                            //第二种,获取ServletAPI,存放
                            HttpSession s=ServletActionContext.getRequest().getSession();
                            s.setAttribute("session", "session888");
                            return SUCCESS;
                        }
                    } 

这里写图片描述

 Stack Context中取数据
	<body>
		<!--取ActionContext中的数据,通过#key读取-->
	    <s:property value="#hello" />
	         -输出:hello session
	    <br>
	    <!--取Session中,通过#key.name map中的key-->
	    <s:property value="#session.hello" /> 
	         -输出:hello session
	    <s:property value="#session.Session" /> 
	         -输出:session888
	    <s:property value="#session" /> 
	        - 输出:{Session=session888, hello=hello session}
	 
	</body>

###Value Stack 中存取数据

    struts2在请求到来时,会创建ValueStack,将当前的Action对象放入栈顶。
    Struts2把ValueStack存放在request中,所以我们可以在request中获取ValueStack对象。
    ValueStack是值栈,先进后出,后进先出。
     存数据 
             public class SaveDemo extends ActionSupport {
                 public String execute(){
                     /*//获取ValueStack对象 
                     HttpServletRequest request=ServletActionContext.getRequest();
                     ValueStack vs1=(ValueStack) request.getAttribute("struts.valueStack");
                     System.out.println(vs1);

                     //第二种获取ValueStack的方法
                     ActionContext context=ActionContext.getContext();
                     Map<String,Object> map=(Map<String, Object>) context.get("request");
                     ValueStack vs2=(ValueStack) map.get("struts.valueStack");
                     System.out.println(vs2);*/

                     //第三种方式获取ValueStack方法
                     ActionContext context=ActionContext.getContext();
                     ValueStack vs3=context.getValueStack(); 
                     //将对象压栈
                     vs3.push(new User("张三",18));
                     return SUCCESS; 
                 }
             } 
    取数据
                //只能取对象中的属性,ValueStack是list集合,里面是一个一个元素,我们只能取元素的属性,比如user对象的username,age等,不能取这个对象
                //由于ValueStack是根对象,取ValueStack中的对象属性时,不使用#。
                //从栈顶开始逐个对象查找指定的属性名称,只要找到就不再继续查找
                <s:property value="username"/>

###Value Stack 案例

我们在我们的Action类中也设置一个username属性
                    public class SaveDemo extends ActionSupport {
                        private String username="李四";
                        public String getUsername() {
                            return username;
                        }
                        public void setUsername(String username) {
                            this.username = username;
                        }
                        public String execute(){

                            ActionContext context=ActionContext.getContext();
                            ValueStack vs3=context.getValueStack();
                            vs3.push(new User("张三",18));


                            return SUCCESS; 
                        }
                    } 
由于我们发送请求时,会将请求的Action实例先压入栈,然后再将我们的对象压栈 
                如图,栈里有两个username,我们怎么取动作类里的username呢?

                可以使用索引来查找,从0开始,代表属性的索引 所以。
                    <s:property value="[0].username"/><br>
                    <s :property value="[1].username"/>

                    输出 :
                            张三
                            李四

###ValueStack 的其他方法

-setValue(String expr,Object value):expr是OGNL表达式,value是数据。这个方法是存放数据,存到哪里看OGNL表达式
     如果OGNL表达式使用#,则存放到ActionContext中 

     没有使用,就存放到ValueStack中 

     vs3.setValue("#username", "王五"); //将数据存到ActionContext中,username是key,王五是值 

     vs3.setValue("username", "赵六");  //将ValueStack中的第一个username替换成赵六。如果类中没有username的set方法。就不会替换和设置。



 -void set(String key,Object o); key是Map的key,o是Map的value。
         这个方法如果栈顶是一个Map元素,就把key作为map的key,把Object作为map的value。设置进去。 

         如果栈顶不是Map元素,则创建一个Map对象,把Key作为map的key,Object作为map的value,压入栈顶。

         vs3.set("user1",new User("刘德华",54) ); 

         在页面中怎么取呢?这是一个Map对象,是key和value,并没有属性 
           <s:property value="user1.username"/> 


         当我们使用<s:property  />元素不指定Value时,默认输出当前栈顶的元素。 

 -findValue(String expr):根据OGNL表达式查找
        其实我们的<s:property  />标签的原理就是这个 
        ValueStack vs=ActionContext.getContext().getValueStack();
        Object obj=vs.findValue(OGNL表达式) 

##struts2 与 ognl 结合的体现
struts2 与 ognl 结合的体现除了体现在上面 struts 标签,还体现在参数接受和配置文件中,我们来了解一下
###参数接受

属性驱动

这里写图片描述

对象驱动

这里写图片描述

模型驱动

这里写图片描述
该 XXXAction 中没有 name 属性,只有 User 属性。如果不将 User 压进栈顶,无法完成赋值。那么,我们我们如何在赋值前把 User 压进栈顶呢?这里就要用到 struts2 的 20 个拦截器中的 prepare 拦截器。该拦截器在 params 拦截器之前,所以我们只要在 action 类中实现 peparable 接口并且实现 prepare() 方法即可。在 prepare() 方法中把 User 压进栈顶即可。
###配置文件中
例如我们在重定向到Action时,需要携带参数,此时就需要用到 ognl 表达式

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

PS:本文部分内容参考于 https://blog.csdn.net/c99463904/article/details/72630304,再整理而来。

附:ValueStack 是一个栈结构,作为一个 root 对象,它所在的上下文对象为 ActionContext。通过 ActionContext 可以得到 ValueStack,通过 ValueStack 也可以得到 ActionContex。OGNL 表达式通过 root 对象操作 ValueStack,也可以操作其上下文对象,也就是 ActionContext,就这样吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值