struts2基础学习五之OGNL表达式
来源:互联网 发布:java cp 命令 编辑:程序博客网 时间:2024/06/03 22:46
1、OGNL表达式static方法调用
首先配置struts.xml文件中的属性
<consant name="struts.ognl.allowStaticMethodAccess" value="true" />如果输出是字符串
<s:property value="'Hello World!!!'"/>使用静态方法
<s:property value="@com.tools.Util@toUpperCase('Hello World')"/>注:
① 在类前使用@
② 在类的方法前也一样使用@
③ 在方法里面也可直接写变量,和正常一样
2、OGNL表达式动态方法调用(动态方法是默认调用的)
① <s:property value="new com.tools.UtilString().subString('Hello World')"/><span style="font-size:18px;"></span><pre name="code" class="html">② <s:property value="new com.tools.UtilString().subString1(new com.tools.UtilString().subString2('Hello World')"/>)"/>③ <s:property value="getUtil().subString('Hello World')"/>
或者
<s:property value="util.subString('Hello World')"/>
④ <s:property value="out('Hello World')"/>
注:
① 就和在Java类中直接写Java代码是一样的
② 也可以在方法内调用new 对象
③ 直接使用getUtil()方法,在OGNL表达式中会认为直接由一个util属性,就会调用这个属性或者直接使用属性是一样的。
④ 也可使用使用方法名,直接调用方法。
3、OGNL(Object Graphic Navigation Language)属于另一个开源框架经过struts2扩展集成struts2框架。OGNL可以用于JSP标签、界面参数传递到Action中,还可以用于struts2配置文件中。OGNL对象图--可以任意一个对象为根,通过OGNL可以访问与这个对象关联的其他对象。Root就是对象图中的根对象,对象图导航必须以getter方法进行导航。
4、OGNL引擎通过setValue和getValue方法进行取值和赋值
package com.test;import static org.junit.Assert.*;import ognl.Ognl;import ognl.OgnlException;import org.junit.Test;import com.model.Address;import com.model.Contact;import com.model.User;public class test {@Testpublic void test() {User user = new User();user.setAge(20);user.setName("zhuge");user.setPassword("sss");Address address = new Address();address.setHome("wojia");user.setAddress(address);Contact contact = new Contact();address.setContact(contact);contact.setEmail("23sdaf");contact.setPhone("13433334324");try {String str = (String) Ognl.getValue("address.contact.email", user); <span style="font-size:18px;"></span><pre name="code" class="html"> //String str = (String) Ognl.getValue("#root.address.contact.email", user);
System.out.println("oldValue:"+str);Ognl.setValue("address.contact.email", user, "newvalue");str = (String) Ognl.getValue("address.contact.email", user);System.out.println("newValue:"+str);} catch (OgnlException e) {e.printStackTrace();System.out.println("Exception...");}}}运行结果:
5、不能导航的对象都放在ActionContext中,从中获取。其实就是一个Map键值对
@Testpublic void test1() {User user1 = new User();user1.setAge(20);user1.setName("李四");user1.setPassword("sss");User user = new User();user.setAge(20);user.setName("王五");user.setPassword("zzz");Map<String,User> context = new HashMap<String,User>();context.put("user1", user1);context.put("user2", user);try {//获取单个String name = (String)Ognl.getValue("#user2.name", context,new Object());//#user2.name的#user就是使用get("user2")获取该对象。//Ognl.getValue("#user2.name", context,new Object());第三个参数是根对象,如果没有或者不写直接写new Object()即可System.out.println("name:"+name);//获取多个,类似于写Java代码System.out.println(Ognl.getValue("#user1.name + ','+#user2.name", context,new Object()));//设置属性值Ognl.setValue("#user2.name", context,new Object(),"张七");System.out.println(Ognl.getValue("#user2.name", context,new Object()));//在键值对Context中必须使用#} catch (OgnlException e) {e.printStackTrace();}}
6、OGNL调用方法
① 调用静态方法:
String name = (String)Ognl.getValue("@java.lang.System@out.println(#user2.name)", context,new Object());
② 调用动态方法:
String name = (String)Ognl.getValue("new String(#user2.name).toString()", context,new Object());
注:OGNL调用方法与JSP页面中调用方法是一致的。
7、OGNL集合使用
① 创建List集合
@Testpublic void test3(){User user1 = new User();user1.setName("李四");try {List list = (List) Ognl.getValue("{'hello',122,name}", user1);System.out.println(list);} catch (OgnlException e) {// TODO Auto-generated catch blocke.printStackTrace();}}访问List集合
Ognl.getValue("@java.lang.System@out.println(#list[2])", context,new Object());
② 创建数组
Object[] obj = (Object[]) Ognl.getValue("new Object[]{'hello',122,name}", user1);System.out.println(obj);for(int i=0;i<obj.length;i++){System.out.println(obj[i]);}访问数组
Ognl.getValue("@java.lang.System@out.println(#obj[1])", context,new Object());
③ 创建Map集合
Map<String,String> map = (Map<String, String>) Ognl.getValue("#{'name':name,'age':20}", user1);System.out.println(map);访问Map集合(两种方式)
Ognl.getValue("@java.lang.System@out.println(#map['name'])", context,new Object());Ognl.getValue("@java.lang.System@out.println(#map.age)", context,new Object());
<span style="font-size:18px;">Ognl.getValue("@java.lang.System@out.println(#map['list'][1])", context,new Object());</span>
8、值栈(ValueStack)
0 0
- struts2基础学习五之OGNL表达式
- struts2基础探索之OGNL表达式详解
- Struts2框架学习之OGNL表达式
- Struts2框架学习之四:OGNL表达式
- struts2之ognl表达式
- struts2之OGNL表达式
- struts2之ognl表达式
- struts2之OGNL表达式
- Struts2学习之OGNL表达式原理(转载学习)
- struts2 - ognl表达式 学习笔记
- struts2 - ognl表达式 学习笔记
- struts2总结之 OGNL表达式
- Struts2之OGNL表达式语言
- 框架学习之struts2-03标签、OGNL表达式
- 【struts2学习之OGNL详解】
- 五、struts2的OGNL的学习
- Struts2 之五:撩开OGNL的面纱
- struts2重点、精华、用法总结(五):数据处理机制之原生OGNL表达式
- Objective C 之文件管理
- android 报错 java.lang.IllegalStateException: Could not find a method next(View)
- Otsu Thresholding
- jetty
- Cocos2d-js官方完整项目教程翻译:五、让主角运行动画
- struts2基础学习五之OGNL表达式
- OpenStack组件高可用
- Eclipse常用快捷键
- Hibernate事务与并发问题处理(乐观锁与悲观锁)
- 如何使用HttpClient3.x
- 普通安装weblogic12.1.3
- 开学第一课——从“心”开始 心理健康教育推广公益活动
- ListControl控件的使用方法
- Apache Mina 学习笔记(3) - IoService