现在的位置: 首页 > 综合 > 正文

一步一步跟我学Struts2 —— OGNL,数据运转的催化剂

2013年12月07日 ⁄ 综合 ⁄ 共 5553字 ⁄ 字号 评论关闭

首先让我们花费1分钟的时间来简单思考一个问题,MVC这3者之间,到底是通过什么真正融合起来的? 

有人说是Controller,因为它是核心控制器,没有Controller,MVC就无从谈起,失去了职责划分的原本初衷。也有人说是View,因为所有的需求都是页面驱动的,没有页面,就没有请求,没有请求,也谈不上控制器和数据模型。 

个人观点:贯穿MVC模型之间起到粘合剂作用的是数据。数据在View层成为了展示的内容,而在Controller层,成为了操作的载体,所以数据是整个MVC的核心。 

流转的数据 

无论MVC三者之间的粘合剂到底是什么,数据在各个层次之间进行流转是一个不争的事实。而这种流转,也就会面临一些困境,这些困境,是由于数据在不同世界中的表现形式不同而造成的: 

1. 数据在页面上是一个扁平的,不带数据类型的字符串,无论你的数据结构有多复杂,数据类型有多丰富,到了展示的时候,全都一视同仁的成为字符串在页面上展现出来。 

2. 数据在Java世界中可以表现为丰富的数据结构和数据类型,你可以自行定义你喜欢的类,在类与类之间进行继承、嵌套。我们通常会把这种模型称之为复杂的对象树。 

此时,如果数据在页面和Java世界中互相流转传递,就会显得不匹配。所以也就引出了几个需要解决的问题: 

1. 当数据从View层传递到Controller层时,我们应该保证一个扁平而分散在各处的数据集合能以一定的规则设置到Java世界中的对象树中去。同时,能够聪明的进行由字符串类型到Java中各个类型的转化。 

2. 当数据从Controller层传递到View层时,我们应该保证在View层能够以某些简易的规则对对象树进行访问。同时,在一定程度上控制对象树中的数据的显示格式。 

如果我们稍微深入一些来思考这个问题,我们就会发现,解决数据由于表现形式的不同而发生流转不匹配的问题对我们来说其实并不陌生。同样的问题会发生在Java世界与数据库世界中,面对这种对象与关系模型的不匹配,我们采用的解决方法是使用ORM框架,例如Hibernate,iBatis等等。那么现在,在Web层同样也发生了不匹配,所以我们也需要使用一些工具来帮助我们解决问题。 

在这里,我们主要讨论的,是数据从View层传递到Controller层时的解决方案,而数据从Controller层传递到View层的解决方案,我们将在Struts2的Result章节重点讨论。 

OGNL —— 完美的催化剂 

为了解决数据从View层传递到Controller层时的不匹配性,Struts2采纳了XWork的OGNL方案。并且在OGNL的基础上,构建了OGNLValueStack的机制,从而比较完美的解决了数据流转中的不匹配性。 

OGNL(Object Graph Navigation Language),是一种表达式语言。使用这种表达式语言,你可以通过某种表达式语法,存取Java对象树中的任意属性、调用Java对象树的方法、同时能够自动实现必要的类型转化。如果我们把表达式看做是一个带有语义的字符串,那么OGNL无疑成为了这个语义字符串与Java对象之间沟通的桥梁。 

如何使用OGNL 

让我们先研究一下OGNL的API,他来自于Ognl的静态方法: 

Java代码  收藏代码
  1. /** 
  2.  * Evaluates the given OGNL expression tree to extract a value from the given root 
  3.  * object. The default context is set for the given context and root via 
  4.  * <CODE>addDefaultContext()</CODE>. 
  5.  * 
  6.  * @param tree the OGNL expression tree to evaluate, as returned by parseExpression() 
  7.  * @param context the naming context for the evaluation 
  8.  * @param root the root object for the OGNL expression 
  9.  * @return the result of evaluating the expression 
  10.  * @throws MethodFailedException if the expression called a method which failed 
  11.  * @throws NoSuchPropertyException if the expression referred to a nonexistent property 
  12.  * @throws InappropriateExpressionException if the expression can't be used in this context 
  13.  * @throws OgnlException if there is a pathological environmental problem 
  14.  */  
  15. public static Object getValue( Object tree, Map context, Object root ) throws OgnlException;  
  16.   
  17. /** 
  18.  * Evaluates the given OGNL expression tree to insert a value into the object graph 
  19.  * rooted at the given root object.  The default context is set for the given 
  20.  * context and root via <CODE>addDefaultContext()</CODE>. 
  21.  * 
  22.  * @param tree the OGNL expression tree to evaluate, as returned by parseExpression() 
  23.  * @param context the naming context for the evaluation 
  24.  * @param root the root object for the OGNL expression 
  25.  * @param value the value to insert into the object graph 
  26.  * @throws MethodFailedException if the expression called a method which failed 
  27.  * @throws NoSuchPropertyException if the expression referred to a nonexistent property 
  28.  * @throws InappropriateExpressionException if the expression can't be used in this context 
  29.  * @throws OgnlException if there is a pathological environmental problem 
  30.  */  
  31. public static void setValue( Object tree, Map context, Object root, Object value ) throws OgnlException  

我们可以看到,OGNL的API其实相当简单,你可以通过传递三个参数来实现OGNL的一切操作。而这三个参数,被我称为OGNL的三要素。 

那么运用这个API,我们能干点什么呢?跑个测试看看结果: 

Java代码  收藏代码
  1. /** 
  2.  * @author Downpour 
  3.  */  
  4. public class User {  
  5.       
  6.     private Integer id;  
  7.       
  8.     private String name;  
  9.       
  10.     private Department department = new Department();  
  11.       
  12.     public User() {  
  13.           
  14.     }  
  15.           
  16.         // setter and getters  
  17. }  
  18.   
  19. //=========================================================================  
  20.   
  21. /** 
  22.  * @author Downpour 
  23.  */  
  24. public class Department {  
  25.       
  26.     private Integer id;  
  27.       
  28.     private String name;  
  29.       
  30.     public Department() {  
  31.           
  32.     }  
  33.           
  34.         // setter and getters  
  35. }  
  36.   
  37. //=========================================================================  
  38.   
  39. /** 
  40.  * @author Downpour 
  41.  */  
  42. public class OGNLTestCase extends TestCase {  
  43.       
  44.     /** 
  45.      *  
  46.      * @throws Exception 
  47.      */  
  48.     @SuppressWarnings("unchecked")  
  49.     @Test  
  50.     public void testGetValue() throws Exception {  
  51.           
  52.         // Create root object  
  53.         User user = new User();  
  54.         user.setId(1);  
  55.         user.setName("downpour");  
  56.   
  57.         // Create context  
  58.         Map context = new HashMap();  
  59.         context.put("introduction","My name is ");  
  60.           
  61.         // Test to directly get value from root object, with no context  
  62.         Object name = Ognl.getValue(Ognl.parseExpression("name"), user);  
  63.         assertEquals("downpour",name);  
  64.           
  65.         // Test to get value(parameter) from context  
  66.         Object contextValue = Ognl.getValue(Ognl.parseExpression("#introduction"), context, user);  
  67.         assertEquals("My name is ", contextValue);  
  68.           
  69.         // Test to get value and parameter from root object and context  
  70.         Object hello = Ognl.getValue(Ognl.parseExpression("#introduction + name"), context, user);  
  71.         assertEquals("My name is downpour",hello);  
  72.                       
  73.     }  
  74.   
  75.     /** 
  76.      *  
  77.      * @throws Exception 
  78.      */  
  79.     @SuppressWarnings("unchecked")  
  80.     @Test  
  81.     public void testSetValue() throws Exception {  
  82.           
  83.         // Create root object  
  84.         User user = new User();  
  85.         user.setId(1);  
  86.         user.setName("downpour");  
  87.           
  88.                 // Set value according to the expression  
  89.         Ognl.setValue("department.name", user, "dev");  
  90.         assertEquals("dev", user.getDepartment().getName());  
  91.           
  92.     }  
  93.       
  94.   

抱歉!评论已关闭.