- 浏览: 359958 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (511)
- AgileMethodology (4)
- RDBMS (32)
- NoSQL (16)
- Java (27)
- Python (28)
- Maven (15)
- Linux (27)
- CommonUtils (13)
- IDE (8)
- JavaScript (17)
- jQuery (2)
- OSGi (2)
- JavaWeb (5)
- Spring (37)
- Struts2 (3)
- ORM (13)
- Ant (1)
- apache-tiles (1)
- FreeMarker (2)
- JSON (8)
- XML (1)
- JUnit (3)
- Lucene (1)
- Web Service (9)
- Design Pattern (13)
- Algorithm (21)
- JVM (14)
- Hadoop (16)
- Spark (5)
- Scala (31)
- Git (4)
- Server (3)
- Node.js (18)
- Concurrent (42)
- Lock (9)
- Collections (3)
- Network (11)
- MicroService (7)
- Docker (13)
- FP (20)
- spring.io (2)
- ELK (1)
- Kafka (5)
最新评论
Person.java
package org.fool.ognl; public class Person { private String name; private Dog dog; public String getName() { return name; } public void setName(String name) { this.name = name; } public Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } }
Dog.java
package org.fool.ognl; public class Dog { private String name; private String[] friends; public String getName() { return name; } public void setName(String name) { this.name = name; } public String[] getFriends() { return friends; } public void setFriends(String[] friends) { this.friends = friends; } }
OgnlTest.java
package org.fool.ognl; import java.util.ArrayList; import java.util.List; import ognl.Ognl; import ognl.OgnlContext; import ognl.OgnlException; /** * OGNL(Object Graph Navigation Language)对象导航语言 * * OgnlContext(上下文对象),存在唯一的叫做根的对象(root), 可以通过程序设定上下文当中哪个对象作为根对象。 * * 在OGNL中,如果表达式没有使用#号,那么OGNL会从根对象中寻找 该属性对应的get方法,如果寻找的不是根对象中的属性, * 那么则需要以#号开头,告诉OGNL,去寻找你所指定的特定对象中的属性。 */ public class OgnlTest { public static void main(String[] args) throws OgnlException { Person person = new Person(); person.setName("zhangsan"); Dog dog2 = new Dog(); dog2.setName("wangcai2"); person.setDog(dog2); Dog dog = new Dog(); dog.setName("wangcai"); OgnlContext context = new OgnlContext(); context.put("person", person); context.put("dog", dog); context.setRoot(person); // 根对象 Object object = Ognl.parseExpression("name"); System.out.println(object); Object object2 = Ognl.getValue(object, context, context.getRoot()); System.out.println(object2); System.out.println("-----------"); Object object3 = Ognl.parseExpression("#person.name"); System.out.println(object3); Object object4 = Ognl.getValue(object3, context, context.getRoot()); System.out.println(object4); System.out.println("-----------"); Object object5 = Ognl.parseExpression("#dog.name"); System.out.println(object5); Object object6 = Ognl.getValue(object5, context, context.getRoot()); System.out.println(object6); System.out.println("-----------"); Object object7 = Ognl.parseExpression("#person.dog.name"); System.out.println(object7); Object object8 = Ognl.getValue(object7, context, context.getRoot()); System.out.println(object8); System.out.println("-----------"); Object object9 = Ognl.parseExpression("name.toUpperCase()"); System.out.println(object9); Object object10 = Ognl.getValue(object9, context, context.getRoot()); System.out.println(object10); System.out.println("-----------"); // 当使用OGNL调用静态方法的时候,需要按照如下语法编写表达式: // @package.classname@methodname(parameter) Object object11 = Ognl .parseExpression("@java.lang.Integer@toBinaryString(10)"); System.out.println(object11); Object object12 = Ognl.getValue(object11, context, context.getRoot()); System.out.println(object12); System.out.println("-----------"); // 对于OGNL来说,java.lang.Math是其的默认类,如果调用java.lang.Math的静态方法时, // 无需指定类的名字,比如:@@min(4, 10) Object object13 = Ognl.parseExpression("@@min(4, 10)"); System.out.println(object13); Object object14 = Ognl.getValue(object13, context, context.getRoot()); System.out.println(object14); System.out.println("-----------"); Object object15 = Ognl.parseExpression("new java.util.LinkedList()"); System.out.println(object15); Object object16 = Ognl.getValue(object15, context, context.getRoot()); System.out.println(object16); // 对于OGNL来说,数组与集合是一样的,都是通过下标索引来去访问的。构造集合的时候使用{...}形式 Object object17 = Ognl.getValue("{'aa', 'bb', 'cc', 'dd'}[3]", context, context.getRoot()); System.out.println(object17); System.out.println("-----------"); dog.setFriends(new String[] { "aa", "bb", "cc" }); Object object18 = Ognl.getValue("#dog.friends[0]", context, context.getRoot()); System.out.println(object18); System.out.println("-----OGNL操纵集合------"); List<String> list = new ArrayList<String>(); list.add("hello"); list.add("world"); list.add("hello world"); context.put("list", list); System.out .println(Ognl.getValue("#list[2]", context, context.getRoot())); System.out.println("-----OGNL操纵映射------"); // 处理OGNL来处理映射(Map)的语法格式如下所示: // #{'key1' : 'value1', 'key2' : 'value2', '...' : '...'} System.out.println(Ognl.getValue( "#{'key1' : 'value1', 'key2' : 'value2'}['key2']", context, context.getRoot())); System.out.println("-----过滤------"); // 过滤(filtering):collection.{? expression} List<Person> persons = new ArrayList<Person>(); Person p1 = new Person(); Person p2 = new Person(); Person p3 = new Person(); p1.setName("zhangsan"); p2.setName("lisi"); p3.setName("wangwu"); persons.add(p1); persons.add(p2); persons.add(p3); context.put("persons", persons); System.out.println(Ognl.getValue( "#persons.{? #this.name.length() > 4}.size()", context, context.getRoot())); System.out.println(Ognl.getValue( "#persons.{? #this.name.length() > 4}.isEmpty()", context, context.getRoot())); // OGNL针对集合提供了一些伪属性(如size, isEmpty),让我们可以通过属性的方式来调用 // 方法(本质原因在与集合当中的很多方法并不符合JavaBean的命名规则),但我们依然可以通 // 过调用方法来实现与伪属性相同的目的。 System.out.println(Ognl.getValue( "#persons.{? #this.name.length() > 4}.size", context, context.getRoot())); System.out.println(Ognl.getValue( "#persons.{? #this.name.length() > 4}.isEmpty", context, context.getRoot())); System.out.println("-----------"); // 过滤(filtering):获取集合中的第一个元素collection.{^ expression} System.out.println(Ognl.getValue( "#persons.{^ #this.name.length() > 4}[0].name", context, context.getRoot())); // 过滤(filtering):获取集合中的最后一个元素collection.{$ expression} System.out.println(Ognl.getValue( "#persons.{$ #this.name.length() > 4}[0].name", context, context.getRoot())); System.out.println("-----投影------"); // 投影(projection):collection.{expression} System.out.println(Ognl.getValue("#persons.{name}", context, context.getRoot())); System.out.println("-----------"); System.out.println(Ognl.getValue("#persons.{#this.name.length() <= 5 ? 'Hello World' : #this.name}", context, context.getRoot())); } }
... <dependencies> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>${struts-version}</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> <version>${struts-version}</version> </dependency> </dependencies> ...
... <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ...
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.devMode" value="true" /> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.objectFactory" value="spring" /> <constant name="struts.i18n.reload" value="true" /> <constant name="struts.configuration.xml.reload" value="true" /> </struts>
Student.java
package org.fool.bean; import java.util.Map; public class Student { private String name; private int age; private String address; private String[] teachers; private Cat cat; private Map<String, String> map; public Student() { } public Student(String name, int age, String address, String[] teachers, Cat cat, Map<String, String> map) { super(); this.name = name; this.age = age; this.address = address; this.teachers = teachers; this.cat = cat; this.map = map; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String[] getTeachers() { return teachers; } public void setTeachers(String[] teachers) { this.teachers = teachers; } public Cat getCat() { return cat; } public void setCat(Cat cat) { this.cat = cat; } public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } }
Cat.java
package org.fool.bean; public class Cat { private String name; private int age; private String color; public Cat() { } public Cat(String name, int age, String color) { this.name = name; this.age = age; this.color = color; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } }
OgnlAction.java
package org.fool.action; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.interceptor.ApplicationAware; import org.apache.struts2.interceptor.RequestAware; import org.apache.struts2.interceptor.SessionAware; import org.fool.bean.Cat; import org.fool.bean.Student; import com.opensymphony.xwork2.ActionSupport; @Namespace("/") @Result(name = "success", location = "ognl.jsp") public class OgnlAction extends ActionSupport implements RequestAware, SessionAware, ApplicationAware { private String username; private String password; private Map<String, Object> requestMap; private Map<String, Object> sessionMap; private Map<String, Object> applicationMap; private List<Student> list = new ArrayList<Student>(); 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 List<Student> getList() { return list; } public void setList(List<Student> list) { this.list = list; } @Override public void setApplication(Map<String, Object> application) { this.applicationMap = application; } @Override public void setSession(Map<String, Object> session) { this.sessionMap = session; } @Override public void setRequest(Map<String, Object> request) { this.requestMap = request; } @Action(value = "ognlAction") public String execute() throws Exception { requestMap.put("hello", "groovy"); sessionMap.put("hello", "grails"); applicationMap.put("hello", "jquery"); Cat cat1 = new Cat("cat1", 20, "red"); Cat cat2 = new Cat("cat2", 30, "blue"); String[] teachers1 = { "hello1", "hello2", "hello3" }; String[] teachers2 = { "world1", "world2", "world3" }; Map<String, String> map1 = new HashMap<String, String>(); Map<String, String> map2 = new HashMap<String, String>(); map1.put("hello1", "hello1"); map1.put("hello2", "hello2"); map2.put("world1", "world1"); map2.put("world2", "world2"); Student student1 = new Student("zhangsan", 20, "beijing", teachers1, cat1, map1); Student student2 = new Student("lisi", 30, "shanghai", teachers2, cat2, map2); list.add(student1); list.add(student2); return SUCCESS; } }
ognl.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%> <%@ page import="com.opensymphony.xwork2.ActionContext" %> <%@ page import="com.opensymphony.xwork2.util.ValueStack" %> <%@ page import="org.fool.action.OgnlAction" %> <%@ page import="java.util.Map" %> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>ognl.jsp</title> </head> <body> username:<s:property value="username"/><br> password:<s:property value="password"/><br> <hr> username:<s:property value="#parameters.username"/><br> password:<s:property value="#parameters.password"/><br> <hr> request:<s:property value="#request.hello"/><br> session:<s:property value="#session.hello"/><br> application:<s:property value="#application.hello"/><br> attr:<s:property value="#attr.hello"/><br> <hr> request:<%= ((Map) ActionContext.getContext().get("request")).get("hello") %><br> session:<%= ActionContext.getContext().getSession().get("hello") %><br> application:<%=ActionContext.getContext().getApplication().get("hello") %><br> attr:<%= ((Map) ActionContext.getContext().get("attr")).get("hello")%> <hr> student1.address:<s:property value="list[0].address"/><br> student2.age:<s:property value="list[1].age"/><br> student1.cat.name:<s:property value="list[0].cat.name"/><br> size:<s:property value="list.size"/><br> isEmpty:<s:property value="list.isEmpty"/><br> <hr> student1.address:<%= ((OgnlAction) ActionContext.getContext().getValueStack().peek()).getList().get(0).getAddress() %><br> student2.age:<%= ((OgnlAction) ActionContext.getContext().getValueStack().peek()).getList().get(1).getAge() %><br> student1.cat.name:<%= ((OgnlAction) ActionContext.getContext().getValueStack().peek()).getList().get(0).getCat().getName() %><br> <hr> student2.friend:<s:property value="list[1].teachers[2]"/><br> student2.friend:<%= ((OgnlAction) ActionContext.getContext().getValueStack().peek()).getList().get(1).getTeachers()[2] %><br> <hr> student2.map2:<s:property value="list[1].map['world2']"/><br> <hr> filtering:<s:property value="list.{? #this.name.length() > 2}[1].name"/><br> <hr> <s:iterator value="list.{? #this.name.length() > 2}"> <s:property value="name"/><br> <s:property value="cat.color"/><br> <s:property value="teachers[0]"/><br> </s:iterator> <hr> projection:<br> <s:iterator value="list.{age}"> <s:property/><br> </s:iterator> </body> </html>
相关推荐
Struts2 OGNL(Object-Graph Navigation Language)是一种强大的表达式语言,它在Struts2框架中扮演着核心角色,用于数据绑定、控制流程以及动态方法调用。这篇博文可能详细介绍了Struts2框架中OGNL的使用、工作原理...
### Struts2中OGNL的使用详解 #### 一、OGNL简介 OGNL(Object-Graph Navigation Language)是一种强大的表达式语言,用于获取或设置一个对象图中的属性。它在Struts2框架中扮演着极其重要的角色,是Struts2实现...
在这个“Struts2 OGNL用法项目”中,我们将深入探讨OGNL(Object-Graph Navigation Language),它是Struts2中一个强大的表达式语言,用于在模型对象和视图之间进行数据绑定和表达式计算。 OGNL是Struts2的核心组件...
OGNL(Object-Graph Navigation Language)是Struts2中的核心表达式语言,用于在Action对象和视图之间传递数据。在这个主题中,我们将深入探讨Struts2 OGNL2.6.11的源码,了解其内部工作原理和关键功能。 首先,...
Struts2 OGNL表达式是Java开发中一个重要的知识点,尤其在Web应用程序设计中扮演着核心角色。OGNL(Object-Graph Navigation Language)是一种强大的表达式语言,它被Struts2框架广泛用于视图层与模型层之间的数据...
Struts2 OGNL表达式是Java Web开发中一个重要的概念,它是一种强大的对象图形导航语言(Object-Graph Navigation Language)。在Struts2框架中,OGNL被广泛用于视图层,作为数据绑定的主要手段,使得开发者能够方便...
struts2 OGNL表达式使用 OGNL(Object-Graph Navigation Language)是对象图导航语言,允许开发者在Struts2应用程序中访问和操作对象及其属性。下面是OGNL表达式的使用方法: 访问基本属性 1. 访问值栈中action的...
Struts2 OGNL语言学习笔记 OGNL(Object-Graph Navigation Language)是 Struts 2 中的一种表达式语言,主要用于简化 JSP 页面中的编码,使页面与后台代码分离。下面是 OGNL 语言的主要特点和用法: 1. 支持对象...
Struts2 OGNL---标签详解析 都有实例 适合初学者
其中,OGNL(Object-Graph Navigation Language)是Struts2中的核心表达语言,用于在视图层与模型层之间传递数据。在深入理解OGNL的源码之前,我们首先需要了解OGNL的基本概念和用法。 OGNL是一种强大的表达式语言...
这个"Struts2 OGNL示例(Maven项目)"提供了使用OGNL与Struts2集成的实例,帮助开发者更好地理解和应用这一强大的特性。 首先,让我们了解什么是OGNL。OGNL是一种强大的表达式语言,允许我们访问和修改对象图中的...
这个压缩包包含的是OGNL的2版本的源代码,这对于理解Struts2框架的工作原理以及OGNL语言的实现细节非常有帮助。 OGNL的主要功能是提供一种简洁的方式来获取和设置对象的属性,甚至可以处理复杂的对象图。例如,你...
关于struts2 OGNL 详细教程,对初学者有帮助
Struts2 OGNL(Object-Graph Navigation Language)表达式是一种强大的、动态的数据绑定和脚本语言,广泛应用于Struts2框架中。它允许开发者在视图层与模型层之间灵活地传递数据,增强了MVC架构中的灵活性。本文将...
8. **Struts2中的OGNL拦截器**:Struts2框架使用了一系列拦截器来增强OGNL的功能,例如ValueStack,它可以将Action上下文中的所有对象暴露给OGNL表达式。这使得在视图层直接访问Action的属性变得简单。 总的来说,...
Struts2 OGNL(Object-Graph Navigation Language)是一种强大的表达式语言,广泛应用于Apache Struts2框架中,用于在视图、控制器和模型之间进行数据的表达和操作。Struts2是Java Web开发中最流行的MVC框架之一,而...
### Struts2 OGNL 表单提交问题详解 #### 一、背景介绍 Struts2框架作为一款流行的企业级应用开发框架,在处理MVC架构方面有着独特的优势。其中,OGNL (Object-Graph Navigation Language) 作为一种强大的表达式...
struts2 ognl源文件 在ECLIPSE导入后可方面的利于开发