- 浏览: 144128 次
- 性别:
- 来自: 上海
-
最新评论
-
lijingshou:
有经验的同学根据自己的情况选一点学习就可以了..当然不叫你从头 ...
java学习者的福音----最强JAVA学习线路图以及各阶段配备的学习神器! -
rex0654335:
博主之前说的看完月薪两三万,改掉了?没底气了?
java学习者的福音----最强JAVA学习线路图以及各阶段配备的学习神器! -
lk557:
stinge 写道广告贴??看完之后感觉是广告么?
java学习者的福音----最强JAVA学习线路图以及各阶段配备的学习神器! -
stinge:
广告贴??
java学习者的福音----最强JAVA学习线路图以及各阶段配备的学习神器! -
yixiandave:
lk557 写道yixiandave 写道Struts2已经开 ...
java学习者的福音----最强JAVA学习线路图以及各阶段配备的学习神器!
Struts2深入学习----OGNL表达式原理
目录
一、OGNL表达式基础知识
OGNL表达式
OGNL,全称为Object-GraphNavigationLanguage,它是一个功能强大的表达式语言,用来获取和设置Java对象的属性,它旨在提供一个更高的更抽象的层次来对Java对象图进行导航。
OGNL表达式的基本单位是"导航链",一般导航链由如下几个部分组成:
1. 属性名称(property)
2. 方法调用(method invoke)
3.数组元素
所有的OGNL表达式都基于当前对象的上下文来完成求值运算,链的前面部分的结果将作为后面求值的上下文。例如:names[0].length()。
public class OGNL1 { public static void main(String[] args) { /* 创建一个Person对象 */ Person person = new Person(); person.setName("zhangsan"); try { /* 从person对象中获取name属性的值 */ Object value = Ognl.getValue("name", person); System.out.println(value); } catch (OgnlException e) { e.printStackTrace(); } } } class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
控制台输出:
zhangsan
可以看到我们正确的取得了person对象的name属性值,该getValue声明如下:
public static <T> T getValue(String expression,Object root)throws OgnlException Convenience method that combines calls to parseExpression and getValue. Parameters: expression - the OGNL expression to be parsed root - the root object for the OGNL expression Returns: the result of evaluating the expression
OGNL会根据表达式从根对象(root)中提取值。
public class OGNL1 { public static void main(String[] args) { /* 创建一个上下文Context对象,它是用保存多个对象一个环境 对象 */ Map<String , Object> context = new HashMap<String , Object>(); Person person1 = new Person(); person1.setName("zhangsan"); Person person2 = new Person(); person2.setName("lisi"); Person person3 = new Person(); person3.setName("wangwu"); /* person4不放入到上下文环境中 */ Person person4 = new Person(); person4.setName("zhaoliu"); /* 将person1、person2、person3添加到环境中(上下文中) */ context.put("person1", person1); context.put("person2", person2); context.put("person3", person3); try { /* 获取根对象的"name"属性值 */ Object value = Ognl.getValue("name", context, person2); System.out.println("ognl expression \"name\" evaluation is : " + value); /* 获取根对象的"name"属性值 */ Object value2 = Ognl.getValue("#person2.name", context, person2); System.out.println("ognl expression \"#person2.name\" evaluation is : " + value2); /* 获取person1对象的"name"属性值 */ Object value3 = Ognl.getValue("#person1.name", context, person2); System.out.println("ognl expression \"#person1.name\" evaluation is : " + value3); /* 将person4指定为root对象,获取person4对象的"name"属性,注意person4对象不在上下文中 */ Object value4 = Ognl.getValue("name", context, person4); System.out.println("ognl expression \"name\" evaluation is : " + value4); /* 将person4指定为root对象,获取person4对象的"name"属性,注意person4对象不在上下文中 */ Object value5 = Ognl.getValue("#person4.name", context, person4); System.out.println("ognl expression \"person4.name\" evaluation is : " + value5); /* 获取person4对象的"name"属性,注意person4对象不在上下文中 */ // Object value6 = Ognl.getValue("#person4.name", context, person2); // System.out.println("ognl expression \"#person4.name\" evaluation is : " + value6); } catch (OgnlException e) { e.printStackTrace(); } } } class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
控制台输出:
ognl expression "name" evaluation is : lisi ognl expression "#person2.name" evaluation is : lisi ognl expression "#person1.name" evaluation is : zhangsan ognl expression "name" evaluation is : zhaoliu ognl.OgnlException: source is null for getProperty(null, "name") at ognl.OgnlRuntime.getProperty(OgnlRuntime.java:2296) at ognl.ASTProperty.getValueBody(ASTProperty.java:114) at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212) at ognl.SimpleNode.getValue(SimpleNode.java:258) at ognl.ASTChain.getValueBody(ASTChain.java:141) at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212) at ognl.SimpleNode.getValue(SimpleNode.java:258) at ognl.Ognl.getValue(Ognl.java:494) at ognl.Ognl.getValue(Ognl.java:596) at ognl.Ognl.getValue(Ognl.java:566) at com.beliefbetrayal.ognl.OGNL1.main(OGNL1.java:53)
对于使用上下文的OGNL,若不指定从哪一个对象中查找"name"属性,则OGNL直接从根对象(root)查找,若指定查找对象(使用'#'号指定,如#person1),则从指定的对象中查找,若指定对象不在上下文中则会抛出异常,换句话说就是是#person1.name形式指定查找对象则必须要保证指定对象在上下文环境中。
public class OGNL2 { public static void main(String[] args) { /* OGNL提供的一个上下文类,它实现了Map接口 */ OgnlContext context = new OgnlContext(); People people1 = new People(); people1.setName("zhangsan"); People people2 = new People(); people2.setName("lisi"); People people3 = new People(); people3.setName("wangwu"); context.put("people1", people1); context.put("people2", people2); context.put("people3", people3); context.setRoot(people1); try { /* 调用 成员方法 */ Object value = Ognl.getValue("name.length()", context, context.getRoot()); System.out.println("people1 name length is :" + value); Object upperCase = Ognl.getValue("#people2.name.toUpperCase()", context, context.getRoot()); System.out.println("people2 name upperCase is :" + upperCase); Object invokeWithArgs = Ognl.getValue("name.charAt(5)", context, context.getRoot()); System.out.println("people1 name.charAt(5) is :" + invokeWithArgs); /* 调用静态方法 */ Object min = Ognl.getValue("@java.lang.Math@min(4,10)", context, context.getRoot()); System.out.println("min(4,10) is :" + min); /* 调用静态变量 */ Object e = Ognl.getValue("@java.lang.Math@E", context, context.getRoot()); System.out.println("E is :" + e); } catch (OgnlException e) { e.printStackTrace(); } } } class People { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
控制台输出:
people1 name length is :8 people2 name upperCase is :LISI people1 name.charAt(5) is :s min(4,10) is :4 E is :2.718281828459045
使用OGNL调用方法也十分简单,对于成员方法调用,只需要给出方法的名称+(),若有参数,直接写在括号内,与一般调用Java方法一致。对于静态方法的调用,需要使用如下格式:@ClassName@method,对于静态变量需要使用如下格式:@ClassName@field。
public class OGNL3 { public static void main(String[] args) throws Exception { OgnlContext context = new OgnlContext(); Classroom classroom = new Classroom(); classroom.getStudents().add("zhangsan"); classroom.getStudents().add("lisi"); classroom.getStudents().add("wangwu"); classroom.getStudents().add("zhaoliu"); classroom.getStudents().add("qianqi"); Student student = new Student(); student.getContactWays().put("homeNumber", "110"); student.getContactWays().put("companyNumber", "119"); student.getContactWays().put("mobilePhone", "112"); context.put("classroom", classroom); context.put("student", student); context.setRoot(classroom); /* 获得classroom的students集合 */ Object collection = Ognl.getValue("students", context, context.getRoot()); System.out.println("students collection is :" + collection); /* 获得classroom的students集合 */ Object firstStudent = Ognl.getValue("students[0]", context, context.getRoot()); System.out.println("first student is : " + firstStudent); /* 调用集合的方法 */ Object size = Ognl.getValue("students.size()", context, context.getRoot()); System.out.println("students collection size is :" + size); System.out.println("--------------------------飘逸的分割线--------------------------"); Object mapCollection = Ognl.getValue("#student.contactWays", context, context.getRoot()); System.out.println("mapCollection is :" + mapCollection); Object firstElement = Ognl.getValue("#student.contactWays['homeNumber']", context, context.getRoot()); System.out.println("the first element of contactWays is :" + firstElement); System.out.println("--------------------------飘逸的分割线--------------------------"); /* 创建集合 */ Object createCollection = Ognl.getValue("{'aa','bb','cc','dd'}", context, context.getRoot()); System.out.println(createCollection); /* 创建Map集合 */ Object createMapCollection = Ognl.getValue("#{'key1':'value1','key2':'value2'}", context, context.getRoot()); System.out.println(createMapCollection); } } class Classroom { private List<String> students = new ArrayList<String>(); public List<String> getStudents() { return students; } public void setStudents(List<String> students) { this.students = students; } } class Student { private Map<String , Object> contactWays = new HashMap<String , Object>(); public Map<String , Object> getContactWays() { return contactWays; } public void setContactWays(Map<String , Object> contactWays) { this.contactWays = contactWays; } }
控制台的输出:
students collection is :[zhangsan, lisi, wangwu, zhaoliu, qianqi] first student is : zhangsan students collection size is :5 --------------------------飘逸的分割线-------------------------- mapCollection is :{homeNumber=110, mobilePhone=112, companyNumber=119} the first element of contactWays is :110 --------------------------飘逸的分割线-------------------------- [aa, bb, cc, dd] {key1=value1, key2=value2}
OGNL不仅可以操作集合对象,还可以创建集合对象,对集合操作与对属性的操作没什么不同,需要注意的是OGNL认为List与Array是一样的。使用OGNL创建List集合时使用{},创建Map对象时使用#{}。
public class OGNL4 { public static void main(String[] args) throws Exception { OgnlContext context = new OgnlContext(); Humen humen = new Humen(); humen.setName("qiuyi"); humen.setSex("n"); humen.setAge(22); humen.getFriends().add(new Humen("zhangsan" , "n" , 22)); humen.getFriends().add(new Humen("lisi" , "f" , 21)); humen.getFriends().add(new Humen("wangwu" , "n" , 23)); humen.getFriends().add(new Humen("zhaoliu" , "n" , 22)); humen.getFriends().add(new Humen("qianqi" , "n" , 22)); humen.getFriends().add(new Humen("sunba" , "f" , 20)); humen.getFriends().add(new Humen("yangqiu" , "f" , 25)); context.put("humen", humen); context.setRoot(humen); /* OGNL过滤集合的语法为:collection.{? expression} */ Object filterCollection = Ognl.getValue("friends.{? #this.name.length() > 7}", context, context.getRoot()); System.out.println("filterCollection is :" + filterCollection); System.out.println("--------------------------飘逸的分割线--------------------------"); /* OGNL投影集合的语法为:collection.{expression} */ Object projectionCollection = Ognl.getValue("friends.{name}", context, context.getRoot()); System.out.println("projectionCollection is :" + projectionCollection); } } class Humen { private String name; private String sex; private int age; private List<Humen> friends = new ArrayList<Humen>(); public Humen() { } public Humen(String name , String sex , int age) { this.name = name; this.sex = sex; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List<Humen> getFriends() { return friends; } public void setFriends(List<Humen> friends) { this.friends = friends; } @Override public String toString() { return "Humen [name=" + name + ", sex=" + sex + ", age=" + age + "]"; } }
控制台输出:
filterCollection is :[Humen [name=zhangsan, sex=n, age=22]] --------------------------飘逸的分割线-------------------------- projectionCollection is :[zhangsan, lisi, wangwu, zhaoliu, qianqi, sunba, yangqiu]
OGNL可以对集合进行过滤与投影操作,过滤的语法为collection.{? expression},其中使用"#this"表示集合当前对象(可以与for-each循环比较)。投影的语法为collection.{expression}。投影和过滤可以看做是数据库中对表取列和取行的操作。
Struts 2支持以下几种表达式语言:
1. OGNL(Object-Graph Navigation Language),可以方便地操作对象属性的开源表达式语言;
2. JSTL(JSP Standard Tag Library),JSP 2.0集成的标准的表达式语言;
3. Groovy,基于Java平台的动态语言,它具有时下比较流行的动态语言(如Python、Ruby和Smarttalk等)的一些起特性;
4. Velocity,严格来说不是表达式语言,它是一种基于Java的模板匹配引擎,具说其性能要比JSP好。
Struts 2默认的表达式语言是OGNL,原因是它相对其它表达式语言具有下面几大优势:
1. 支持对象方法调用,如xxx.doSomeSpecial();
2. 支持类静态的方法调用和值访问,表达式的格式为@[类全名(包括包路径)]@[方法名 | 值名],例如:@java.lang.String@format('foo %s', 'bar')或@tutorial.MyConstant@APP_NAME;
3. 支持赋值操作和表达式串联,如price=100, discount=0.8, calculatePrice(),这个表达式会返回80;
4. 访问OGNL上下文(OGNL context)和ActionContext;
5. 操作集合对象。
——————————————————————以上内容引用自http://www.blogjava.net/max/archive/2007/04/28/114417.html
平时使用Struts2标签时会出现一些很奇特的问题,对于OGNL不了解的人可能对问题的出现无能为力或者就算解决了问题也不知道是如何解决的。下面总结一些使用Struts2标签容易出现的困惑:
在Struts2标签属性中经常会出现"#"或者"%{}"的符号出现,通过上面OGNL表达式基础的介绍,知道了OGNL上下文中有且仅有一个根对象。Struts2为我们定义了许多明明对象,他们分别是"ValueStack","Parameters","Session","Request", "Appliction","Attr",其中"ValueStack"被设置为上下文的根对象。访问非根对象必须加上"#"号,这就是出现"#"的原因。Struts2中的标的处理类,并不是所有都将标签的属性作为OGNL表达式来看待,有时候我们需要设置动态地值,则必须告诉标签的处理类该字符串按照OGNL表达式来处理,%{}符号的作用就是告诉标签的处理类将它包含的字符串按照OGNL表达式处理。 "$"符号用于XML文件中用于获取动态值,与%{}作用类似。
Struts2的标签几十几百个,要记住哪一个标签的处理类将标签的属性作为OGNL表达式是一件很困难的事情,在不清楚处理类的处理方式时怎么办,%{}对于标签处理类来说,若处理类将属性值作为普通字符串则%{}符号包含的字符串当做OGNL表达式,若处理类将属性值作为OGNL表达式来处理,则直接忽略%{}符号。换句话说,不清楚处理方式的话,可以都使用%{}符号。
下面是ValueStack的官方描述:
ValueStack allows multiple beans to be pushed in and dynamic EL expressions to be evaluated against it. When evaluating an expression, the stack will be searched down the stack, from the latest objects pushed in to the earliest, looking for a bean with a getter or setter for the given property or a method of the given name (depending on the expression being evaluated).
大致意思:ValueStack允许保存多个bean(也就是Action),并且可以使用表达式语言获得他们。当评估一个表达式,ValueStack将会从栈顶到栈底的方向被搜索一遍,对于给定的属性名称寻找bean的getter或setter方法或寻找给定的方法。
每当一个请求到达Action时,Struts2会将Action对象推入ValueStack中。
<body> username:<s:property value="username"/><br /> -------------------诡异的分割线-------------------<br /> username:<%= ((HelloWorldAction)ActionContext.getContext().getValueStack().peek()).getUsername() %><br /> </body>
页面显示结果:
username:zhangsan -------------------诡异的分割线------------------- username:zhangsan
可以看到标签取值与用Java代码取值的结果相同,明显标签的取值方式更简练简洁。OGNL表达式"username"表示了从根对象ValueStack中取出属性username的值。它会从栈顶到栈底遍历ValueStack,直到找某一个Action中的"username"属性。
相关推荐
这个压缩包包含了这三个关键组件的源代码,对于开发者来说,它提供了深入理解Struts2框架工作原理的宝贵资源。 `struts2-core-2.3.7-sources.jar` 是Struts2核心库的源代码,其中包含了许多关键组件,如Action、...
对于初学者,可以通过阅读这些源码来学习如何使用OGNL表达式,如何编写Action和拦截器,以及如何配置Struts2的XML配置文件。对于有经验的开发者,源码分析有助于找出性能瓶颈,优化代码,提高系统的稳定性和安全性。...
Struts2是一个流行的Java web应用程序框架,它极大地简化了MVC(模型-视图-控制器)架构的实现。...如果你想要深入学习Struts2和OGNL,可以参考给出的博客链接,或者查阅官方文档和其他相关的技术资源。
1. `s:property`: 用于显示Action对象或OGNL表达式的结果。例如,`<s:property value="username" />`可以显示用户姓名。 2. `s:textfield`: 创建输入文本字段,自动绑定到Action中的属性。如`邮箱" />`。 3. `s:...
通过Javadoc,开发者可以学习到如何使用OGNL表达式来绑定模型数据到视图,或者在控制器中执行条件判断和循环等操作。OGNL的灵活性和强大功能使得Struts2具有高度的数据绑定能力。 在Eclipse这样的集成开发环境中,...
深入学习这些源代码,开发者不仅可以了解OGNL的内部工作机制,还能提升对Struts2框架的理解,从而更好地设计和调试应用程序。同时,这也有助于学习其他类似的表达式语言或脚本引擎,因为很多概念和设计模式都是通用...
在深入学习Struts2 OGNL表达式时,了解以下几点至关重要: 1. **OGNL基础语法**:包括变量引用、属性访问、集合操作、方法调用等。 2. **Struts2动作和结果**:如何在配置文件中使用OGNL定义Action和结果。 3. **...
6. **OGNL表达式语言(OGNL)**:Struts2使用OGNL作为默认的表达式语言,用于在Action和视图之间传递数据。`ognl`包中的类和接口实现了OGNL的解析和执行。 7. **异常处理(Exception Handling)**:在`struts2-core...
2. **实验和调试**:创建示例项目,实践使用OGNL表达式,观察其行为,同时通过调试源码来理解其工作过程。 3. **学习文档**:官方文档会详细介绍OGNL的语法和用法,是学习的基础。 4. **参与社区**:加入OGNL相关的...
"struts2对Ognl的封装--PropertyAccessor"这个主题主要涉及Struts2框架如何处理OGNL表达式,特别是如何通过PropertyAccessor接口来访问和操作对象的属性。 首先,我们来了解什么是OGNL。OGNL是Struts2的核心组件之...
这个压缩包“struts2-showcase.rar”包含了完整的源代码,旨在帮助开发者深入理解Struts2框架的工作原理及其各种特性。以下是对Struts2和Struts2-showcase的详细解释: **Struts2框架** Struts2是一个基于MVC...
通过深入学习OGNL的源码,开发者可以更好地定制和优化Struts2应用,提升性能,增强安全性,并能解决遇到的特定问题。这是一项值得投入时间和精力的任务,特别是对于那些希望在Web开发领域有深入理解的人来说。
通过学习和实践这些内容,开发者可以深入理解Struts2框架的工作原理,掌握其配置和使用方法,从而在实际项目中更好地应用Struts2,提升开发质量和效率。同时,了解如何在Eclipse中进行Struts2项目的配置和调试,也是...
5. `ognl.jar`:OGNL(Object-Graph Navigation Language)是Struts2的默认表达式语言,用于在视图层和模型层之间传递数据。 6. `freemarker.jar`:FreeMarker是Struts2默认使用的模板引擎,用于生成动态HTML页面。...
### 精通Struts2中的OGNL表达式 在深入探讨Struts2框架的核心功能时,理解并熟练掌握OGNL(Object-Graph Navigation Language)表达式的应用是至关重要的一步。OGNL是一种强大的表达式语言,它允许开发者通过简洁、...
Struts2是一个流行的Java web应用程序...学习OGNL不仅可以提高在Struts2中的开发效率,还能为其他使用OGNL的项目提供参考。如果你对某个特定部分有更深入的兴趣,例如表达式解析或安全防护,可以进一步探索相关源码。
- XWork的`OgnlValueStack`是表达式语言OGNL(Object-Graph Navigation Language)在Struts2中的实现,用于在Action和视图之间传递数据。 - 类型转换是XWork的一个重要特性,它可以自动将请求参数转换为Action字段...
此外,熟悉OGNL表达式和结果类型也是掌握Struts2的关键。 总之,Struts2-2.5.13是一个成熟的Java Web框架,提供了许多开箱即用的功能和高度的可定制性,是企业级应用开发的有力工具。通过深入学习和实践,开发者...
### JS:ognl表达式详解 #### 一、OGNL表达式概述 OGNL (Object-Graph Navigation Language) 是一种强大的表达式语言,用于获取和设置 Java 对象的属性。它最初是由 John Rose 和 Luke Blanshard 开发,并被广泛...