`
sundful
  • 浏览: 1237696 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

ognl总结

    博客分类:
  • OGNL
 
阅读更多
	// ***************** root对象的概念 ******************* //
	public void testOgnl_01() throws Exception{
		User user = new User();
		user.setUsername("张三");
		
		//相当于调用user.getUsername()方法
		String value = (String)Ognl.getValue("username", user);
		System.out.println(value);
	}
	
	public void testOgnl_02() throws Exception{
		User user = new User();
		Person p = new Person();
		p.setName("张三");
		user.setPerson(p);
		
		//相当于调用user.getPerson().getName()方法
		String value = (String)Ognl.getValue("person.name", user);
		System.out.println(value);
	}
	
	public void testOgnl_03() throws Exception{
		User user = new User();
		Person p = new Person();
		p.setName("张三");
		user.setPerson(p);
		
		//可以使用#root来引用根对象,相当于调用user.getPerson().getName()方法
		String value = (String)Ognl.getValue("#root.person.name", user);
		System.out.println(value);
	}
	

 

 

// *********************** context的概念 **********************//
	public void testOgnl_04() throws Exception{
		Person p1 = new Person();
		Person p2 = new Person();
		p1.setName("张三");
		p2.setName("李四");
		
		Map context = new HashMap();
		context.put("p1", p1);
		context.put("p2", p2);
		
		String value = (String)Ognl.getValue("#p1.name + ',' + #p2.name", context, new Object());
		System.out.println(value);
	}
	
	public void testOgnl_05() throws Exception{
		Person p1 = new Person();
		Person p2 = new Person();
		p1.setName("张三");
		p2.setName("李四");
		
		Map context = new HashMap();
		context.put("p1", p1);
		context.put("p2", p2);
		
		User root = new User();
		root.setUsername("zhangsan");
		
		String value = (String)Ognl.getValue("#p1.name + ',' + #p2.name + ',' + username", context, root);
		System.out.println(value);
	}

 

 

 

	// ******************* OGNL赋值操作 ************************//
	public void testOgnl_06() throws Exception{
		User user = new User();
		
		//给root对象的属性赋值,相当于调用user.setUsername()
		Ognl.setValue("username", user, "zhangsan");
		
		System.out.println(user.getUsername());
	}
	
	public void testOgnl_07() throws Exception{
		User user = new User();
		
		Map context = new HashMap();
		context.put("u", user);
		
		//给context中的对象属性赋值,相当于调用user.setUsername()
		Ognl.setValue("#u.username",context, new Object(), "zhangsan");
		
		System.out.println(user.getUsername());
	}
	
	public void testOgnl_08() throws Exception{
		User user = new User();
		
		Map context = new HashMap();
		context.put("u", user);
		
		//给context中的对象属性赋值,相当于调用user.setUsername()
		//在表达式中使用=赋值操作符来赋值
		Ognl.getValue("#u.username = '张三'",context, new Object());
		
		System.out.println(user.getUsername());
	}
	
	public void testOgnl_09() throws Exception{
		User user = new User();
		Person p = new Person();
		
		Map context = new HashMap();
		context.put("u", user);
		
		context.put("p", p);
		
		//给context中的对象属性赋值,相当于调用user.setUsername()
		//在表达式中使用=赋值操作符来赋值
		Ognl.getValue("#u.username = '张三',#p.name = '李四'",context, new Object());
		
		System.out.println(user.getUsername()+","+p.getName());
	}
	

 

 

 

	
	//****************** 使用OGNL调用对象的方法 **********************//
	public void testOgnl_10() throws Exception{
		User user = new User();
		user.setUsername("张三");
		
		String value = (String)Ognl.getValue("getUsername()", user);
		System.out.println(value);
	}
	
	public void testOgnl_11() throws Exception{
		User user = new User();
		
		Ognl.getValue("setUsername('张三')", user);
		System.out.println(user.getUsername());
	}
	

 

 

 

	// ********************* OGNL中的this表达式 **********************//
	public void testOgnl_14() throws Exception{
		Object root = new Object();
		Map context = new HashMap();
		
		List values = new ArrayList();
		for(int i=0; i<11; i++){
			values.add(i);
		}
		context.put("values", values);
		
		Ognl.getValue("@System@out.println(#values.size.(#this > 10 ? \"大于10\" : '不大于10'))", context, root);
		
	}
	
	public void testOgnl_15() throws Exception{
		User user = new User();
		
		Ognl.getValue("setUsername('ZHANGSAN')", user);
		Ognl.getValue("@System@out.println(#this.username)", user);
	}
	
	public void testOgnl_16() throws Exception{
		User user = new User();
		
		Ognl.getValue("setUsername('ZHANGSAN')", user);
		Ognl.getValue("@System@out.println(username.(#this.toLowerCase()))", user);
	}

 

 

	// ******************* 如何把表达式的解释结果作为另外一个表达式,OGNL中括号的使用 **********************//
	public void testOgnl_17() throws Exception{
		Object root = new Object();
		Map context = new HashMap();
		User u = new User();
		u.setUsername("张三");
		context.put("u", u);
		context.put("fact", "username");
		
		/**
		 * 1、首先把#fact表达式进行解释,得到一个值:username
		 * 2、解释括号中的表达式#u,其结果是user对象
		 * 3、把括号中表达式的结果作为root对象,解释在第一步中得到的结果(即把第一步的结果看成另外一个表达式) 
		 */
		String value = (String)Ognl.getValue("#fact(#u)", context, root);
		System.out.println(value);
	}
	
	public void testOgnl_18() throws Exception{
		Person person = new Person();
		Map context = new HashMap();
		User u = new User();
		u.setUsername("张三");
		context.put("u", u);
		
		/**
		 * 相当于调用person这个根对象的fact方法,并把#u的解释结果作为参数传入此方法 
		 */
		String value = (String)Ognl.getValue("fact(#u)", context, person);
		System.out.println(value); //输出是 "张三,"
	}
	
	public void testOgnl_19() throws Exception{
		Person person = new Person();
		Map context = new HashMap();
		User u = new User();
		u.setUsername("张三");
		context.put("u", u);
		
		/**
		 * 1、先计算表达式fact,得到结果是:username
		 * 2、解释括号中的表达式#u,其结果是user对象
		 * 3、把括号中表达式的结果作为root对象,解释在第一步中得到的结果(即把第一步的结果看成另外一个表达式)
		 */
		String value = (String)Ognl.getValue("(fact)(#u)", context, person);
		System.out.println(value); //输出"张三"
	}
	

 

 

	// ********************* OGNL访问集合元素 **************************//
	public void testOgnl_20() throws Exception{
		Object root = new Object();
		Map context = new HashMap();
		
		//用OGNL创建List对象
		List listvalue = (List)Ognl.getValue("{123,'kdjfk','oooo'}", context, root);
		context.put("listvalue", listvalue);
		
		//用OGNL创建数组
		int[] intarray= (int[])Ognl.getValue("new int[]{23,45,67}", context, root);
		context.put("intarray", intarray);
		
		//用OGNL创建Map对象
		Map mapvalue = (Map)Ognl.getValue("#{'listvalue':#listvalue,'intarray':#intarray}", context, root);
		context.put("mapvalue", mapvalue);
		
		Ognl.getValue("@System@out.println(#listvalue[0])", context, root);
		Ognl.getValue("@System@out.println(#intarray[1])", context, root);
		Ognl.getValue("@System@out.println(#mapvalue['intarray'][2])", context, root);
		Ognl.getValue("@System@out.println(#mapvalue.intarray[0])", context, root);
	}

 

 

分享到:
评论

相关推荐

    很全面的struts2_ognl总结

    ognl语言表达式总结,超详细的,每一个细节都经过验证通过

    OGNL表达式总结

    OGNL表达式总结,个人学习笔记,包含尽可能多的OGNL标签的使用方法,实例等,供大家学习使用

    struts2 标签 OGNL

    有对struts2标签和OGNL的总结。

    理解OGNL 表达式

    这是我自己在学习过程对OGNL的理解,分享给新手学习,只想做点贡献,如有总结错误的地方,请见谅,并提示,改正,谢谢。

    struts2 Ognl表单提交问题

    里面总结了使用ognl做jsp页面时表单提交的一些常见技巧,看了你就知道,很好的

    Velocity语法以及整合struts2总结

    文档详细描述了Velocity整合struts2步骤,以及velocity的语法规范

    Struts2框架学习总结【自用】【原创】【详细】

    个人struts2框架学习后的梳理和总结,内容丰富,从配置文件到action类、result、ognl、类型转换器、国际化、拦截器、表单验证等等内容,还推荐了很多博客链接,有了它自学Struts2框架毫无压力!

    Struts2总结-2

    总结了ognl , 值栈,向值栈中放数据获取数据,拦截器,国际化资源的使用,校验器

    Strust知识点总结

    本资料是Struts2的知识点总结,可以快速回顾Struts的知识点。主要包括:拦截器、结果视图、OGNL、类型转换、国际化、上传下载

    Struts2开发实例总结

    Struts2项目开发总结 (注:Struts2版本:Struts2.1.6,数据库:Oracle9i) 所须架包: commons-logging-1.0.4.jar、commons-fileupload-1.2.1.jar 、freemarker-2.3.13.jar ognl-2.6.11.jar、struts2-core-2.1.6....

    西安领航核心项目Struts2重点、难点总结

    对Struts2框架中的相关知识还有困惑的同学有福了,此次上传的是西安领航何足道老师的核心项目Struts2部分的重点难点的归纳总结,他对Struts2理解非常深刻,讲的非常的详细易懂,堪称经典。主要包括的知识有Action的...

    ibatis和mybatis的区别

    在里面总结了ibatis和mybatis的主要区别,包括xml文件等

    struts2中的ongl表达式相关简介

    OGNL是Struts 2框架的默认表达式语言,增强了Struts 2的数据访问能力,同时简化了代码。这里对ONGL语言进行一个整体的总结

    thymeleaf是spring推荐的模板引擎,这是总结的使用方法

    th:text 指令也可以使用 OGNL 表达式来访问 Controller 传递的数据,例如: ```html 用户名:' + ${user.username}"&gt; ``` 2.Th:utext th:utext 指令用于显示带有 HTML 标签的文本,例如: ```html ${msg}"&gt; ``` ...

    Struts2帮助```````

    03 OGNL表达式语言 23 04 Struts2-Tags 28 Struts2标签目录 28 一、 property标签 28 二、 set标签 29 三、 bean标签 29 四、 标签-少使用 29 五、 If elseif else 30 六、 Iterator标签 31 七、 Theme 31 05设计...

    struts2(2).docx

    struts2的请求数据封装,OGNL基本语法和Struts2表单验证等一些基础概念总结和理解,自己考试复习用,不严谨不客观。

    struts2 学习重点笔记

    这是学习struts2时记得重点笔记,包括了一些原理,ognl语句的编写,以及如何设置拦截器等等一些基本知识,起到复习和巩固的作用

    Struts2 学习笔记

    03 OGNL表达式语言 23 04 Struts2-Tags 28 Struts2标签目录 28 一、 property标签 28 二、 set标签 29 三、 bean标签 29 四、 标签-少使用 29 五、 If elseif else 30 六、 Iterator标签 31 七、 Theme 31 05设计...

    java从入门到精通70个PPT

    36 类型转换和OGNL 37-40 项目案例:在线投票系统 41 jsp servlet struts总结 42 Hibernate 入门 43 Hibernate 关联映射 44 HQL实用技术 45 HQL高级 46 Criteria 查询 47-49 项目实战 影院信息查询系统 50 pl/sql 51...

    Web框架技术期末复习提纲.doc

    10. Struts2 中常用的表达式语言是 OGNL。 11. Struts2 中根据用户语言环境在页面显示不同语言的是国际化。 12. 加载国际化资源文件时使用的拦截器是 fileUpdate。 13. 加载文件上传时使用的拦截器是 fileUpdate。 ...

Global site tag (gtag.js) - Google Analytics