OGNL取值范围分两部分,root、Context两部分

可以放置任何对象作为ROOT,CONTEXT中必须是Map键值对

示例:

准备工作:

    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());
//在""中书写OGNL表达式即可
}

User类:

package bean;

public class User {
private String name;
private Integer age; public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
} }

语法:

从root中取出对象:

        // 取出root中user对象的name属性
String name = (String) Ognl.getValue("name", oc, oc.getRoot());
Integer age = (Integer) Ognl.getValue("age", oc, oc.getRoot());

从Context中取出对象:

        // 取出context中键为user1对象的name属性
String name1 = (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());

为属性赋值:

        // 将root中的user对象的name属性赋值
Ognl.getValue("name='jerry'", oc, oc.getRoot());

给context赋值:

        String name2 = (String) Ognl.getValue("#user1.name='张三'", oc, oc.getRoot());

调用对象的方法:

        // 调用root中user对象的setName方法
Ognl.getValue("setName('张三')", oc, oc.getRoot());
String name1 = (String) Ognl.getValue("getName()", oc, oc.getRoot()); String name2 = (String) Ognl.getValue("#user1.setName('lucy'),#user1.getName()", oc, oc.getRoot());

(注意:可以将两条语句写在一起,逗号分隔,返回值是最后一个语句)

调用静态方法:

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

创建对象:

        // 创建list对象
Integer size = (Integer) Ognl.getValue("{'tom','jerry','jack','rose'}.size()", oc, oc.getRoot());
String name1 = (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()); // 创建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());

OGNL表达式和Struts2结合原理:

OGNLContext本质上就是Struts2中的值栈(ValueStack

ValueStack中有两部分:ROOT、CONTEXT

(队列:先进先出、栈:先进后出)

ROOT部分是一个栈,CONTEXT部分是ActionContext

ROOT中的这个栈本质是一个容器(集合)

例如用list集合制作栈结构容器:push压栈方法  list.add(0,a);、pop弹栈方法  list.remove(0),就可以模拟一个栈

Struts2中有一个类:CompoundRoot类就是一个栈,实现原理和上面类似

用栈的原因:在取栈中的属性时候,会从栈顶开始找属性,找不到继续向下找,找到停止

默认情况下栈中放置的是当前访问的Action对象

OGNL表达式和Struts2结合的体现:

Struts2有一个拦截器params,作用是将获得的参数交给OGNL处理

struts2框架学习笔记5:OGNL表达式的更多相关文章

  1. Struts2学习笔记(OGNL表达式)

    Struts 2支持以下几种表达式语言: OGNL(Object-Graph Navigation Language),可以方便地操作对象属性的开源表达式语言: JSTL(JSP Standard T ...

  2. j2ee开发之struts2框架学习笔记

    Struts2框架技术重点笔记 1.Struts2 是在webwork基础上发展而来. 2.Struts2 不依赖struts API和 servlet API 3.Struts2提供了拦截器,表现层 ...

  3. [ SSH框架 ] Struts2框架学习之三(OGNl和ValueStack值栈学习)

    一.OGNL概述 1.1 什么是OGNL OGNL的全称是对象图导航语言( object-graph Navigation Language),它是一种功能强大的开源表达式语言,使用这种表达式语言,可 ...

  4. struts2框架学习笔记7:struts2标签

    三大标签: 1.JSP:脚本,为了替代servlet,已过时 2.JSTL:标准标签库(core.format.sql.xml),还未淘汰的只有core库 3.Struts2标签库:由Struts2开 ...

  5. Struts2框架学习笔记--strtus2初识

    struts2概述: 1.struts2框架应用于javaEE三层结构中的Web层框架 2.struts2框架是在struts1和webwork基础之上发展的全新框架(脱胎换骨 ,用法完全不一样)ps ...

  6. struts2框架学习笔记1:搭建测试

    Servlet是线程不安全的,Struts1是基于Servlet的框架 而Struts2是基于Filter的框架,解决了线程安全问题 因此Struts1和Struts2基本没有关系,只是创造者取名问题 ...

  7. Struts2框架学习笔记1

    1,框架概述 1.1,什么是框架(了解) 将一些重复性的代码进行封装,简化程序员的编程操作,可以使得程序员在编码中把更多的精力放到业务需求的分析和理解上面,相当于一个半成品软件. 1.2,三大框架(掌 ...

  8. struts2框架学习笔记6:拦截器

    拦截器是Struts2实现功能的核心部分 拦截器的创建: 第一种: package interceptor; import com.opensymphony.xwork2.ActionInvocati ...

  9. struts2框架学习笔记4:获取参数

    第一种参数获取方式: 编写一个前端页面,提交表单,做示例: <form action="${pageContext.request.contextPath}/Demo1Action&q ...

随机推荐

  1. centos安装tree命令

    centos安装tree命令 sudo yum -y install tree windows安装tree命令 我的另一篇

  2. 使用jQuery+huandlebars遍历中if判断

    兼容ie8(很实用,复制过来,仅供技术参考,更详细内容请看源地址:http://www.cnblogs.com/iyangyuan/archive/2013/12/12/3471227.html) & ...

  3. Linux jdk安装

    Linux上一般会安装Open JDK,关于OpenJDK和JDK的区别:http://www.cnblogs.com/sxdcgaq8080/p/7487369.html 下面开始安装步骤: --- ...

  4. python入门(五):切片列表元祖字典

    1.切片 针对序列,使用切片可以获得我们想要的内容 序列:字符串.列表.元祖 特点:可以使用坐标获取某一个值.坐标是从0开始算 >>> s="0123456789" ...

  5. 微信H5支付 遇到坑的一些解决方法

    解决办法 1. 商家参数格式有误,请联系商家解决 a.对于前后端分离的开发模式 前端发起请求 服务端请求微信h5支付统一下单接口 返回参数mweb_url 给前端 然后前端调起微信h5支付 b.注意的 ...

  6. Python自动化测试用例设计--自动化测试用例与手工测试用例区别与联系

    1. 前言 手工测试用例是针对手工测试人员,自动化测试用例是针对自动化测试框架,前者是手工测试用例人员应用手工方式进行用例解析,后者是应用脚本技术进行用例解析,两者最大的各自特点在于,前者具有较好的异 ...

  7. 37 【kubernetes】搭建dashboard

    官方的操作步骤是:https://github.com/kubernetes/dashboard 我自己的步骤是: 1,下载yaml文件 wget https://raw.githubusercont ...

  8. 742. Closest Leaf in a Binary Tree查找最近的叶子节点

    [抄题]: Given a binary tree where every node has a unique value, and a target key k, find the value of ...

  9. 3U - 算菜价

    妈妈每天都要出去买菜,但是回来后,兜里的钱也懒得数一数,到底花了多少钱真是一笔糊涂帐.现在好了,作为好儿子(女儿)的你可以给她用程序算一下了,呵呵. Input 输入含有一些数据组,每组数据包括菜种( ...

  10. latex相关概念

    关于Latex,收到网友的鼓励,决定好好整理下相关的信息. 在初次使用相关的程序时,遇到很多迷惑的概念,下面这篇帖子汇总得很详细. 关于latex各种概念与理解 帖子中提到了三个概念,引擎,宏集(即下 ...