【SSH】Struts2学习(三)OGNL、OGNL与Struts2结合
来源:互联网 发布:aspcms三合一网站源码 编辑:程序博客网 时间:2024/05/16 11:43
一、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//准备RootUser rootUser = new User("tom",18);//准备ContextMap<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);//书写OGNLOgnl.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//准备RootUser rootUser = new User("tom",18);//准备ContextMapcontext = 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);//书写OGNLOgnl.getValue("", oc, oc.getRoot());}@Test//基本语法演示//取出root中的属性值public void fun2() throws Exception{//准备ONGLContext//准备RootUser rootUser = new User("tom",18);//准备ContextMap 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//准备RootUser rootUser = new User("tom",18);//准备ContextMap 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//准备RootUser rootUser = new User("tom",18);//准备ContextMap 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);//书写OGNLOgnl.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//准备RootUser rootUser = new User("tom",18);//准备ContextMap 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);//书写OGNLOgnl.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//准备RootUser rootUser = new User("tom",18);//准备ContextMap 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);//书写OGNLString 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|mappublic void fun7() throws Exception{//准备ONGLContext//准备RootUser rootUser = new User("tom",18);//准备ContextMap 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//创建listInteger 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);//创建mapInteger 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
- 【SSH】Struts2学习(三)OGNL、OGNL与Struts2结合
- Struts2学习(三)—OGNL
- struts2与ognl结合实现下载
- struts2与ognl结合实现下载
- Struts2学习笔记(OGNL)
- Struts2学习——(六)值栈与OGNL表达式[结合struts标签]
- Struts2 OGNL 学习
- struts2的ognl学习
- Struts2 OGNL学习知识
- struts2学习笔记----OGNL
- Struts2 OGNL学习知识
- Struts2与OGNL
- Struts2与OGNL表达式
- Struts2与OGNL表达式
- struts2与OGNL表达式
- struts2学习三(OGNL和ValueStack值栈)
- Struts2学习笔记(三)——ognl
- Struts2(9)——struts2与OGNL的结合(二)
- java基础:文件编码方式
- VC Afx全局函数、数据类型、字符串转化函数(转)
- Windows API-GDI入门基础知识详解(转)
- VC最常用操作程序20项列举(转)
- java内存模型
- 【SSH】Struts2学习(三)OGNL、OGNL与Struts2结合
- C程序优化
- 详解 CORS 跨域资源共享
- 初级 WINDOWS API C++语言版 编程(转)
- poj 3352(tarjan)
- Windows程序的基本结构(转)
- 杂项汇总-Maven篇(持续集成中...)
- 消息映射的实现(转)
- windows消息宏(转)