第 4 章 属性编辑器,数据绑定,校验与BeanWeapper(Bean封装)

4.1. 简介

是否需要对业务逻辑进行验证是一个常见的问题。 有关这一点存在两种截然想法的回答,Spring提出的验证模式(和数据绑定)对两者都不排斥。 验证应该是可以定制化的并且能够依附于任何的验证框架而不应该被强制绑定在Web层。 基于以上原因,Spring提供了一个Validator接口,这个接口可以被应用于应用程序的任何一个层面 (表示层或者逻辑层等 译者注)

数据绑定可以使用户输入与应用程序的域模型进行动态绑定(或者用于处理用户输入对象)。 针对这一点Spring提供了所谓的DataBinder(数据绑定)。 DataBinder和Validator组成了验证包(validation),它主要被用于MVC结构, 除此之外也可以被用于其他的地方。

BeanWrapper是Spring架构的一个基本组件,它在很多地方都是很有用的。 然而,你可能很少直接使用BeanWrapper。 由于这是一篇参考文档,所以我们觉得对此稍作解释还是有必要的。 因为今后你也许进行对象与数据之间的绑定操作,到时就会用到BeanWrapper 了。

Srping大量的使用了PropertyEditors(属性编辑)。 它属于JavaBeans规范的一部分。就像我们上面提到的BeanWrapper一样, PropertyEditors和BeanWrapper以及DataBinder三者之间有着密切的联系。

4.2. 使用DataBinder进行数据绑定

DataBinder构建于BeanWrapper之上。[2].

4.3. Bean处理与BeanWrapper

org.springframework.beans包遵循Sun发布的JavaBeans标准。 一个JavaBean是一个简单的包含无参数构造函数的类,并且包含seter和getter属性方法。 例如prop属性对应setProp(...)方法和getProp()方法. 如果需要了解JavaBeans的详细信息可以访问Sun的网站(java.sun.com/products/javabeans).

这个包中一个非常重要的概念就是BeanWrapper 接口以及它的实现(BeanWrapperImpl)。 根据JavaDoc中的说明,BeanWrapper提供了设置和获得属性值的功能(单个的或者是批量的), 可以获得属性描述、查询只读或者可写属性。而且,BeanWrapper还支持嵌套属性, 可以不限制嵌套深度的进行子属性的设置。所以,BeanWrapper支持标准JavaBeans的PropertyChangeListeners 和VetoableChangeListeners。除此之外,BeanWrapper还提供了设置索引属性的支持。 一般情况下不在应用程序中直接使用BeanWrapper而是使用DataBinderBeanFactory。 从BeanWrapper的名字就可以看出:它封装了一个bean的行为,比如设置和取出属性值。

The way the BeanWrapper works is partly indicated by its name: it wraps a bean to perform actions on that bean, like setting and retrieving properties.

4.3.1. 设置和提取属性以及嵌套属性

设置和提取属性可以通过使用重载的setPropertyValue(s)getPropertyValue(s) 方法来完成。 在Srping的JavaDoc中对它们有详细的描述。有关这两个方法的一些约定习惯如下所列:

表 4.1. 属性示例

语法 解释
name 指出与 getName() 或 isName() 和 setName()相关的name信息
account.name 提供嵌套属性的name,比如getAccount().setName() 或getAccount().getName()方法
account[2] Indicates the third element of the indexed property account. Indexed properties can be of type arraylist or other naturally ordered collection

在下面的例子中你将看到一些使用BeanWrapper设置属性的例子。

注意:如果你不打算直接使用BeanWrapper这部分不是很重要。 如果你仅仅使用DataBinder 和BeanFactory或者他们的扩展实现, 你可以跳过这部分直接阅读PropertyEditors的部分。

考虑下面的两个类:

public class Company {private String name;private Employee managingDirector;public String getName() { return this.name; }public void setName(String name) { this.name = name; } public Employee getManagingDirector() { return this.managingDirector; }public void setManagingDirector(Employee managingDirector) {this.managingDirector = managingDirector;}
}
public class Employee {private float salary;public float getSalary() {return salary;}public void setSalary(float salary) {this.salary = salary;}
}

下面的代码显示了如何接收和设置上面两个类的属性 : Companies and Employees

Company c = new Company();
BeanWrapper bwComp = BeanWrapperImpl(c);
// setting the company name...
bwComp.setPropertyValue("name", "Some Company Inc.");
// ... can also be done like this:
PropertyValue v = new PropertyValue("name", "Some Company Inc.");
bwComp.setPropertyValue(v);// ok, let's create the director and tie it to the company:
Employee jim = new Employee();
BeanWrapper bwJim = BeanWrapperImpl(jim);
bwJim.setPropertyValue("name", "Jim Stravinsky");
bwComp.setPropertyValue("managingDirector", jim);// retrieving the salary of the managingDirector through the company
Float salary = (Float)bwComp.getPropertyValue("managingDirector.salary");

4.3.2. 内建的(PropertyEditors)和类型转换

Spring大量的使用了PropertyEditors。有时候它比对象自身描述属性更加容易。 比如,当我们转换一个日期类型(date)时,可以把它描述成一个用户更容易理解的样子。 这一过程可以通过注册一个自定义编辑器来实现。 java.beans.PropertyEditor.注册一个自定义编辑器告诉BeanWrapper我们将要把属性转换为哪种类型。 你可以从Sun的JavaDoc中 java.beans包了解有关java.beans.PropertyEditor的细节。

下面是几个在Spring中设置属性的例子

  • 使用PropertyEditors设置Bean属性。 当你使用在XML文件中声明的java.lang.String作为bean的属性时, Spring将使用ClassEditor来尝试获得类对象的参数

  • 在Spring MVC架构中传输HTTP请求参数,将使用各种PropertyEditors, 因此你可以绑定各种CommandController的子类。 Spring提供了很多内建的属性编辑器来让这些操作变得简单。 所有这些都列在下面的表格中,你也可以从org.springframework.beans.propertyeditors包中找到它们:

Spring has a number of built-in PropertyEditors to make life easy. Each of those is listed below and they are all located in the org.springframework.beans.propertyeditors package:

表 4.2. Built-in PropertyEditors

Class Explanation
ClassEditor Parses Strings representing classes to actual classes and the other way around. When a class is not found, an IllegalArgumentException is thrown
FileEditor Capable of resolving Strings to File-objects
LocaleEditor Capable of resolving Strings to Locale-objects and vice versa (the String format is [language]_[country]_[variant], which is the same thing the toString() method of Locale provides
PropertiesEditor Capable of converting Strings (formatted using the format as defined in the Javadoc for the java.lang.Properties class) to Properties-objects
StringArrayPropertyEditor Capable of resolving a comma-delimited list of String to a String-array and vice versa
URLEditor Capable of resolving a String representation of a URL to an actual URL-object

Spring使用java.beans.PropertyEditorManager来为属性编辑器设置搜索路径, 这一点时必须的。搜索路径同时包括了sun.bean.editors,这个类包含了前面提到的PropertyEditors ,颜色以及所有原始类型。

4.3.3. 其他特性

除了前面提到的特性,下面还有一些有价值的特性。

  •  确定可读能力和可写能力:使用isReadable() 和 isWritable()方法你可以确定一个属性是否为可读或者可写。

  •  获得属性描述(PropertyDescriptors):使用getPropertyDescriptor(String) 和 getPropertyDescriptors()方法可以获得属性的描述(java.beans.PropertyDescriptor),有时候这会派上用场。

from: http://docs.huihoo.com/spring/zh-cn/validation.html

Spring - Java/J2EE Application Framework 应用框架 第 4 章 属性编辑器,数据绑定,校验与BeanWeapper(Bean封装)相关推荐

  1. Spring - Java/J2EE Application Framework 应用框架 第 5 章 Spring AOP: Spring之面向方面编程G

    第 5 章 Spring AOP: Spring之面向方面编程 5.1. 概念 面向方面编程 (AOP) 提供从另一个角度来考虑程序结构以完善面向对象编程(OOP). 面向对象将应用程序分解成 各个层 ...

  2. Spring - Java/J2EE Application Framework 应用框架 第 13 章 集成表现层

    第 13 章 集成表现层 13.1. 简介 Spring之所以出色的一个原因就是将表现层从MVC的框架中分离出来.例如,通过配置就可以让Velocity或者XSLT来代替已经存在的JSP页面.本章介绍 ...

  3. Spring - Java/J2EE Application Framework 应用框架 第 5 章 Spring AOP: Spring之面向方面编程

    第 5 章 Spring AOP: Spring之面向方面编程 5.1. 概念 面向方面编程 (AOP) 提供从另一个角度来考虑程序结构以完善面向对象编程(OOP). 面向对象将应用程序分解成 各个层 ...

  4. Spring - Java/J2EE Application Framework 应用框架 第 8 章 源代码级的元数据支持

    第 8 章 源代码级的元数据支持 8.1. 源代码级的元数据 源代码级的元数据是对程序元素:通常为类和/或方法的 attribute 或者叫annotation的扩充. 举例来说,我们可以象下面一样给 ...

  5. Spring - Java/J2EE Application Framework 应用框架 第 3 章 Beans, BeanFactory和ApplicationContext

    第 3 章 Beans, BeanFactory和ApplicationContext 3.1. 简介 在Spring中,两个最基本最重要的包是 org.springframework.beans 和 ...

  6. Spring - Java/J2EE Application Framework 应用框架 第 1 章 简介

    第 1 章 简介 1.1. 概览 Spring包含许多功能和特性,并被很好地组织在下图所示的七个模块中.本节将依次介绍每个模块. Spring框架概览 Core包是框架的最基础部分, 并提供依赖注入( ...

  7. Spring - Java/J2EE Application Framework 应用框架 第 11 章 使用ORM工具进行数据访问

    第 11 章 使用ORM工具进行数据访问 11.1. 简介 Spring在资源管理,DAO实现支持以及实物策略等方面提供了与Hibernate, JDO和iBATIS SQL映射的集成. 对Hiber ...

  8. Spring - Java/J2EE Application Framework 应用框架 第 10 章 使用JDBC进行数据访问

    第 10 章 使用JDBC进行数据访问 10.1. 简介 Spring提供的JDBC抽象框架由core, datasource,object和support四个不同的包组成. 就和它名字的暗示一样,o ...

  9. Spring - Java/J2EE Application Framework 应用框架 第 7 章 事务管理

    第 7 章 事务管理 7.1. Spring事务抽象 Spring提供了一致的事务管理抽象.这个抽象是Spring最重要的抽象之一, 它有如下的优点: 为不同的事务API提供一致的编程模型,如JTA. ...

最新文章

  1. VUE -- 自定义控件(标签)
  2. Socket通信实例
  3. C#比较数组内元素相等-冒泡
  4. python学习笔记(四)字典(dict)
  5. [ios2]苹果iOS 5限制应用本地存储问题 【转】
  6. Quartz 定时器任务调度
  7. YbtOJ#662-交通运输【线段树合并,树状数组】
  8. java8 函数式编程_使用Javaslang进行Java 8中的函数式编程
  9. 【图解漏洞】图解跨站请求伪造(CSRF)原理
  10. xp怎么删除计算机用户,谁了解xp系统如何删除工作组计算机
  11. TransferQueue实现线程通信
  12. java 中的随机数
  13. mysql日期查询_mysql 查询日期
  14. 北京内推 | 百度增强现实技术部招聘虚拟人算法实习生
  15. ASM-第二章寄存器
  16. 计算机与汉字+输入速度+云输入法,学拼音打字练习
  17. NodeJS 中上传图片,并且在数据库中保存图片地址
  18. c语言程序设计第二张答案,自学考试《C语言程序设计》习题及答案_第2页
  19. Non-OK-status: Status(error::Code::INVALID_ARGUMENT, “Unsupported data format“) status: Invalid argu
  20. 解决mac BigSur外接显示器发白、发黄、字体发虚 (OpenCore关闭SIP和Read-Only System)

热门文章

  1. http反向代理之haproxy详解
  2. redis High Availability---Redis Sentinel翻译
  3. 图数据库应用:金融反欺诈实践
  4. 专访平安科技首席科学家肖京:平安智能化的甜蜜与辛酸
  5. 【深度学习】caffe 中的一些参数介绍
  6. Google和eBay在建设微服务生态系统中的深刻教训
  7. Navicat 远程连接docker容器中的mysql 报错1251 - Client does not support authentication protocol 解决办法
  8. 改变vim注释的颜色
  9. Apache ZooKeeper - 使用原生的API操作ZK
  10. Redis进阶-如何从海量的 key 中找出特定的key列表 Scan详解