【SSH】Struts2学习(三)OGNL、OGNL与Struts2结合

17 篇文章 2 订阅

一、OGNL

1、OGNL概述

OGNL是Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言,通过它简单一致的表达式语法,可以存取对象的任意属性,调用对象的方法,遍历整个对象的结构图,实现字段类型转化等功能。它使用相同的表达式去存取对象的属性。

${user.addr.name}这种写法就叫对象视图导航,OGNL不仅仅可以视图导航,支持比EL更加丰富的功能

2、OGNL准备工作


(1)导包,struts2已经自带,无需重复导
(2)代码准备
@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());
	}

3、OGNL基本语法
package com.sh.a_ognl;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import com.sh.bean.User;

import ognl.Ognl;
import ognl.OgnlContext;

//展示OGNL语法
public class Demo {
	
	@Test
	//准备工作
	public void fun1() throws Exception{
		//准备ONGLContext
			//准备Root
			User rootUser = new User("tom",18);
			//准备Context
			Map
     
     
      
       context = new HashMap
      
      
       
       ();
			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
       
       
        
         context = new HashMap
        
        
         
         ();
			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
         
         
           context = new HashMap 
          
            (); 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("#user1.name", oc, oc.getRoot()); Integer age = (Integer) Ognl.getValue("#user1.age", oc, oc.getRoot()); System.out.println(name+":"+age); } @Test //基本语法演示 //为属性赋值 public void fun4() throws Exception{ //准备ONGLContext //准备Root User rootUser = new User("tom",18); //准备Context Map 
           
             context = new HashMap 
            
              (); 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 Ognl.getValue("name='jerry'", oc,oc.getRoot()); String name = (String) Ognl.getValue("name", oc, oc.getRoot()); System.out.println(name); Ognl.getValue("#user1.name='麦客子'", oc,oc.getRoot()); String name1 = (String) Ognl.getValue("#user1.name", oc, oc.getRoot()); System.out.println(name1); } @Test //基本语法演示 //调用方法 public void fun5() throws Exception{ //准备ONGLContext //准备Root User rootUser = new User("tom",18); //准备Context Map 
             
               context = new HashMap 
              
                (); 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 Ognl.getValue("setName('孙悟空')", oc, oc.getRoot()); String name = (String) Ognl.getValue("getName()", oc, oc.getRoot()); System.out.println(name); String name1 = (String) Ognl.getValue("#user1.setName('猪八戒'),#user1.getName()", oc, oc.getRoot()); System.out.println(name1); } @Test //基本语法演示 //调用静态方法 public void fun6() throws Exception{ //准备ONGLContext //准备Root User rootUser = new User("tom",18); //准备Context Map 
               
                 context = new HashMap 
                
                  (); 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("@com.sh.a_ognl.HahaUtils@echo('hello java')", oc, oc.getRoot()); /*double name1 = (double) Ognl.getValue("@ java.lang.Math@PI", oc, oc.getRoot());*/ double name1 = (double) Ognl.getValue("@@PI", oc, oc.getRoot()); System.out.println(name); System.out.println(name1); } @Test //基本语法演示 //ognl创建对象list|map public void fun7() throws Exception{ //准备ONGLContext //准备Root User rootUser = new User("tom",18); //准备Context Map 
                 
                   context = new HashMap 
                  
                    (); 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("{'宝钗','黛玉','晴雯'}.size()", oc, oc.getRoot()); String name = (String) Ognl.getValue("{'宝钗','黛玉','晴雯'}[0]", oc, oc.getRoot()); String name2 = (String) Ognl.getValue("{'宝钗','黛玉','晴雯'}[0]", oc, oc.getRoot()); System.out.println(size); System.out.println(name); System.out.println(name2); //创建map Integer size2 = (Integer) Ognl.getValue("#{'name':'宝钗','age':'18'}.size()", oc, oc.getRoot()); String name3 = (String) Ognl.getValue("#{'name':'宝钗','age':'18'}['name']", oc, oc.getRoot()); Integer age = (Integer) Ognl.getValue("#{'name':'宝钗','age':18}.get('age')", oc, oc.getRoot()); System.out.println(size2); System.out.println(name3); System.out.println(age); } } 
                   
                  
                 
                
               
              
             
            
           
         
        
        
       
       
      
      
     
     

二、Struts2与OGNL表达式的结合

1、结合原理


查看值栈两部分内容
Root

Context


2、栈的原理


3、浅尝值栈


(1)值栈的内部结构

在ValueStack的实现类OngValueStack中包含两部分,分别是值栈和Map



(2)ActionContext与ValueStrack的关系




4、Struts2与OGNL结合体现

(1)参数接收





获得值栈的2种方式:

(2)配置文件中


配置文件:

(3)Struts2标签

详见【SSH】Struts2学习(四)

参考源码:https://github.com/AmazeLee/Struts2.git

If we believe that tomorrow will be better, we can bear a hardship today. 

如果我们相信明天会更好,今天就能承受艰辛。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

麦客子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值