Struts2学习(五)
表达式
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() {
} }
转载请于明显处标明出处
Struts2学习(五)的更多相关文章
- Struts2学习五----------指定多个配置文件
© 版权声明:本文为博主原创文章,转载请注明出处 指定多个配置文件 - 在Struts2配置文件中使用include可指定多个配置文件 实例 1.项目结构 2.pom.xml <project ...
- [原创]java WEB学习笔记75:Struts2 学习之路-- 总结 和 目录
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [原创]java WEB学习笔记66:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加) 使用 paramsPrepareParamsStack 重构代码 ,PrepareInterceptor拦截器,paramsPrepareParamsStack 拦截器栈
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- TweenMax动画库学习(五)
目录 TweenMax动画库学习(一) TweenMax动画库学习(二) TweenMax动画库学习(三) Tw ...
- Struts2学习笔记⑧
今天是Struts2学习笔记的最后一篇文章了.用什么做结尾呢,这两天其实还学了很多东西,没有记录下,今天就查漏补缺一下. 文件上传与下载.FreeMarker以及昨天没做完的例子 文件上传与下载 文件 ...
- Struts2学习笔记①
Struts2 学习笔记① 所有的程序学习都从Hello World开始,今天先跟着书做一个HW的示例. Struts2是一套MVC框架,使用起来非常方便,接触到现在觉得最麻烦的地方是配置文件.我的一 ...
- Struts2学习笔记NO.1------结合Hibernate完成查询商品类别简单案例(工具IDEA)
Struts2学习笔记一结合Hibernate完成查询商品类别简单案例(工具IDEA) 1.jar包准备 Hibernate+Struts2 jar包 struts的jar比较多,可以从Struts官 ...
- Struts2学习:interceptor(拦截器)的使用
对于需要登陆验证.权限验证等功能的网站,每一次请求,每一个action都写一段验证的代码,未免显得冗余且不易维护.struts2提供了拦截器interceptor,为这些页面提供一个切面,或者说公共组 ...
- SVG 学习<五> SVG动画
目录 SVG 学习<一>基础图形及线段 SVG 学习<二>进阶 SVG世界,视野,视窗 stroke属性 svg分组 SVG 学习<三>渐变 SVG 学习<四 ...
- Android JNI学习(五)——Demo演示
本系列文章如下: Android JNI(一)——NDK与JNI基础 Android JNI学习(二)——实战JNI之“hello world” Android JNI学习(三)——Java与Nati ...
随机推荐
- URLEncode和URLDecode
URLEncode.encode(String s,String utf-8) 编码 URLDEcode.decode(String %2b%,String utf-8) 解码 用法: String ...
- Flask 学习之flask入门
一.Flask的简单介绍 Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请 ...
- matplotlib动态图subplots()和subplot()不同及参数
一.fig,ax = subplots(nrows,ncols,sharex,sharey,squeeze,subplot_kw,gridspec_kw,**fig_kw) 创建画布和子图 nrow ...
- java8下 枚举 通用方法
在项目中经常用到枚举作为数据字典值和描述的相互转化. 用法如下: public enum CommunicationParamsCom { COM_1(1, "COM1"), CO ...
- 操作word
package com.gwt.flow.task; import java.io.File; import java.io.FileInputStream; import java.io.FileN ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 M. Longest subsequence(思维+序列自动机)
序列自动机跑s串 假设k为s和t相同的长度,初始时相同长度为0 取s串中大于t[i]的最左边的位置,用n-tmp+1+i-1更新答案,tmp是最左端的位置 然后去t[i]相等的位置,走到下一位,如果下 ...
- docker安装后启动报错
docker安装后启动不起来: 查看日志 /var/log/message 其中有一行为: Your kernel does not support cgroup memory limit ...
- 8,xhtml和html有什么区别
8,xhtml和html有什么区别 功能上有差别:xhtml可以兼容各大浏览器,手机,以及pda,浏览器也能快速准确地翻译网页 书写嘻惯的差别:xhtml必须正确的嵌套,闭合,区分大小写,文档必须有根 ...
- 【PAT甲级】1101 Quick Sort (25 分)
题意: 输入一个正整数N(<=1e5),接着输入一行N个各不相同的正整数.输出可以作为快速排序枢纽点的个数并升序输出这些点的值. trick: 测试点2格式错误原因:当答案为0时,需要换行两次
- 「CF891C」Envy
传送门 Luogu 解题思路 考虑最小生成树的几个性质: 所有最小生成树中边权相等的边的条数相等 在任意一颗最小生成树中,边权相等的边所联通的点集一定 那么我们考虑把边权相等的边单独拿出来考虑. 每次 ...