表达式

1、表达式语言 ( Expression Language )
2、表达式的本质:  就是 按照某种规则 书写的 字符串
3、表达式的处理: 一定有一套程序 对 表达式 中的 字符串进行解析和处理,比如 Tomcat
4、常见的表达式

  • JSP 容器中支持 EL 表达式 ( 因此在 JSP 页面中可以直接使用 )
  • Spring 中 可以使用 Spring Expression Language ( SpEL )
  • Struts2 中可以使用 对象图导航语言Object Graphic Navigation Language(OGNL)

OGNL

1、定义

  • OGNL 是一种 功能强大 的 表达式语言 ( Expression Language )
  • OGNL 在 非 Web 环境下 ,需要用到 :javassist-3.20.0-GA.jar,ognl-3.1.12.jar
  • OGNL 目前是 Apache 基金会 下的 commons 项目 下的 一个小项目: http://commons.apache.org

2、测试案例

  • OgnlContext两个方法测试案例

    package ecut.OGNL.entity;
    
    import java.io.Serializable;
    
    public class Customer implements Serializable {
    
        private static final long serialVersionUID = 7499870485797565944L;
    
        private String username ;
    private String password ;
    private String confirm ; public String getUsername() {
    return username;
    }
    public void setUsername(String username) {
    this.username = username;
    }
    public String getPassword() {
    return password;
    }
    public void setPassword(String password) {
    this.password = password;
    }
    public String getConfirm() {
    return confirm;
    }
    public void setConfirm(String confirm) {
    this.confirm = confirm;
    } }
    package ecut.OGNL;
    
    import java.util.Map;
    
    import ecut.OGNL.entity.Customer;
    import ognl.Ognl;
    import ognl.OgnlContext;
    import ognl.OgnlException; public class OgnlTest1 { public static void main(String[] args) throws OgnlException {
    /*OGNL是通常要结合Struts 2的标志一起使用,如<s:property value="#xx" />
    struts页面中不能单独使用,el可以单独使用 ${sessionScope.username} */
    OgnlContext context = new OgnlContext();
    //OgnlContext 实现了Map接口
    System.out.println( context instanceof Map ); // Ognl.setValue( expression , root, value );
    Ognl.setValue( "customer" , context , new Customer() ); // Customer c = new Customer(); context.put( "customer" , c ) ; Ognl.setValue( "customer.username" , context , "张三丰" ); // c.setUsername( "张三丰" ); // JSP 表达式 : <%= c.getUsername() %>
    // EL 表达式: ${ customer.username }
    // OGNL 表达式 : customer.username(支持OGNL的环境,才可以使用) Object value = Ognl.getValue( "customer.username" , context ) ; // Ognl.getValue(expression, root)
    System.out.println( value ); } }

    OGNL 在 非 Web 环境下需要添加两个jar包,OGNL是通常要结合Struts 2的标志一起使用,如<s:property value="#x" /> struts页面中不能单独使用,el可以单独使用 ${sessionScope.username}。OgnlContext实现了Map接口因此可以使用相应的方法获取属性和设置属性。

  • 单元测试测试案例
    package ecut.OGNL;
    
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test; import ecut.OGNL.entity.Customer;
    import ognl.Ognl;
    import ognl.OgnlContext;
    import ognl.OgnlException; public class OgnlTest2 { private OgnlContext root; /* @Before 每次test都执行一次,@BeforeClass仅执行一次,方法得是静态的 */
    public @Before void init() throws OgnlException {
    root = new OgnlContext();
    Ognl.setValue("customer", root, new Customer());
    Ognl.setValue("customer.username", root, "张三丰");
    } public @Test void testGetValue() throws OgnlException {
    Object value = Ognl.getValue("customer.username", root);
    System.out.println(value);
    } public @Test void testSetValue() throws OgnlException {
    Object value = Ognl.getValue("customer.username", root);
    System.out.println(value);
    Ognl.setValue("customer.username", root, "张君宝");
    value = Ognl.getValue("customer.username", root);
    System.out.println(value);
    } public @After void destory() {
    } }

    首先要添加Junit 的library,然后新建Junit Test Case ,注解的位置可以在方法前也可以在修饰符后面。

  • 获取对象属性测试案例
    package ecut.OGNL.action;
    
    import ecut.OGNL.entity.Customer;
    
    public class CustomerAction {
    
        private Customer customer ;
    
        public String execute() throws Exception {
    System.out.println( "execute" );
    return "execute" ;
    } public String login() throws Exception {
    System.out.println( "login" );
    return "login" ;
    } public String logout() throws Exception {
    System.out.println( "logout" );
    return "logout" ;
    } public Customer getCustomer() {
    System.out.println( "CustomerAction # getCustomer()" );
    return customer;
    } public void setCustomer(Customer customer) {
    System.out.println( "CustomerAction # setCustomer( Customer )" );
    this.customer = customer;
    } }
    package ecut.OGNL;
    
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test; import ecut.OGNL.entity.Customer;
    import ecut.OGNL.action.CustomerAction;
    import ognl.Ognl;
    import ognl.OgnlContext;
    import ognl.OgnlException; public class OgnlTest3 { private OgnlContext root ; public @Before void init() throws OgnlException {
    root = new OgnlContext();
    Ognl.setValue( "customerAction" , root , new CustomerAction() );
    } public @Test void testGetValue() throws OgnlException {
    Object value = Ognl.getValue( "customerAction.customer" , root ) ;
    System.out.println( value );
    } public @Test void testSetValue() throws OgnlException {
    Object value = Ognl.getValue( "customerAction.customer" , root ) ;
    System.out.println( value );
    Ognl.setValue( "customerAction.customer" , root , new Customer() );
    Ognl.setValue( "customerAction.customer.username" , root , "张翠山" );
    value = Ognl.getValue( "customerAction.customer.username" , root ) ;
    System.out.println( value );
    } public @After void destory() {
    } }
  • 获取方法测试案例
    package ecut.OGNL;
    
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test; import ecut.OGNL.action.CustomerAction;
    import ognl.Ognl;
    import ognl.OgnlContext;
    import ognl.OgnlException; public class OgnlTest4 { private OgnlContext root ; public @Before void init() throws OgnlException {
    root = new OgnlContext();
    Ognl.setValue( "customerAction" , root , new CustomerAction() );
    } public @Test void testGetValue() throws OgnlException {
    Object value = Ognl.getValue( "customerAction.login()" , root ) ;
    System.out.println( value );
    } public @Test void testSetValue() throws OgnlException {
    } public @After void destory() {
    } }

转载请于明显处标明出处

https://www.cnblogs.com/AmyZheng/p/9217438.html

Struts2学习(五)的更多相关文章

  1. Struts2学习五----------指定多个配置文件

    © 版权声明:本文为博主原创文章,转载请注明出处 指定多个配置文件 - 在Struts2配置文件中使用include可指定多个配置文件 实例 1.项目结构 2.pom.xml <project ...

  2. [原创]java WEB学习笔记75:Struts2 学习之路-- 总结 和 目录

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  3. [原创]java WEB学习笔记66:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加) 使用 paramsPrepareParamsStack 重构代码 ,PrepareInterceptor拦截器,paramsPrepareParamsStack 拦截器栈

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  4. TweenMax动画库学习(五)

    目录            TweenMax动画库学习(一)            TweenMax动画库学习(二)            TweenMax动画库学习(三)            Tw ...

  5. Struts2学习笔记⑧

    今天是Struts2学习笔记的最后一篇文章了.用什么做结尾呢,这两天其实还学了很多东西,没有记录下,今天就查漏补缺一下. 文件上传与下载.FreeMarker以及昨天没做完的例子 文件上传与下载 文件 ...

  6. Struts2学习笔记①

    Struts2 学习笔记① 所有的程序学习都从Hello World开始,今天先跟着书做一个HW的示例. Struts2是一套MVC框架,使用起来非常方便,接触到现在觉得最麻烦的地方是配置文件.我的一 ...

  7. Struts2学习笔记NO.1------结合Hibernate完成查询商品类别简单案例(工具IDEA)

    Struts2学习笔记一结合Hibernate完成查询商品类别简单案例(工具IDEA) 1.jar包准备 Hibernate+Struts2 jar包 struts的jar比较多,可以从Struts官 ...

  8. Struts2学习:interceptor(拦截器)的使用

    对于需要登陆验证.权限验证等功能的网站,每一次请求,每一个action都写一段验证的代码,未免显得冗余且不易维护.struts2提供了拦截器interceptor,为这些页面提供一个切面,或者说公共组 ...

  9. SVG 学习<五> SVG动画

    目录 SVG 学习<一>基础图形及线段 SVG 学习<二>进阶 SVG世界,视野,视窗 stroke属性 svg分组 SVG 学习<三>渐变 SVG 学习<四 ...

  10. Android JNI学习(五)——Demo演示

    本系列文章如下: Android JNI(一)——NDK与JNI基础 Android JNI学习(二)——实战JNI之“hello world” Android JNI学习(三)——Java与Nati ...

随机推荐

  1. C 语言实例 - 判断闰年

    用户输入年份,判断该年份是否为闰年. #include <stdio.h> int main() { int year; printf("输入年份: "); scanf ...

  2. 【转】VMware Converter迁移linux系统虚拟机

    原始出处 今天接到一个需求,迁移一台linux的业务系统到vCenter云管理平台,其中遇到一些问题,于是进行了排错,这个过程与大家分享,下面把整个步骤进行截图说明. 1. 首先,登录到VMware  ...

  3. 每天进步一点点------入门视频采集与处理(BT656简介)

    凡是做模拟信号采集的,很少不涉及BT.656标准的,因为常见的模拟视频信号采集芯片都支持输出BT.656的数字信号,那么,BT.656到底是何种格式呢?      本文将主要介绍 标准的 8bit B ...

  4. jmeter实现IP欺骗

    用jmeter模拟多个IP同时向一个目标发送请求 1.IP地址参数化 在csv文件中编辑参数化IP地址列表,参数化的IP需在同一个局域网,子网掩码相同(比如和客户端本机同一网段),如下 将csv列表中 ...

  5. 常用的vi的命令模式下的快捷命令

    知识点 在VI命令模式下:y 表示拷贝, d 表示删除,p标识粘贴 1.删除 dw 表示删除从当前光标到光标所在单词结尾的内容. d0 表示删除从当前光标到光标所在行首的内容. d$ 表示删除从当前光 ...

  6. 南京邮电大学网络攻防训练平台(NCTF)-异性相吸-Writeup

    南京邮电大学网络攻防训练平台(NCTF)-异性相吸-Writeup 题目描述 文件下载地址 很明显,文件之间进行亦或就可得到flag,不再多说,直接上脚本 #coding:utf-8 file_a = ...

  7. java.util.Properties类,保存时保留注释及格式不变

    原文地址:http://blog.csdn.net/benbenxiongyuan/article/details/53006097 参考地址:http://www.iteye.com/topic/1 ...

  8. Java开发中使用模拟接口moco响应中文时乱码

    场景 在开发中需要依赖一些接口,比如需要请求一个返回Json数据的接口,但是返回Json数据的接口要么是没搭建,要么是交互比较复杂. 此时,就可以使用moco来模拟接口返回接口数据,以便开发和测试工作 ...

  9. JS高级---构造函数,实例对象和原型对象,三者关系

    构造函数,实例对象和原型对象,三者关系 构造函数里面有原型(prototype)属性,即原型对象 原型对象里的constryctor构造器指向构造函数 通过构造函数,实例化,创建的就是实例对象. 实例 ...

  10. RHEL6-HA集群在VMware虚拟机环境安装配置文档

    (一)系统环境描述 本文档基于RHEL6u5 系统安装,配置为2节点高可用集群,节点为两台VMware虚拟机. 也可参考http://blog.51cto.com/ty1992/1325327 (二) ...