Spring Framework 开发参考手册 之四 属性编辑器…

2008-02-23 09:50:00来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

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

ClassExplanationClassEditorParses Strings representing classes to actual classes and the other way around. When a class is not found, an IllegalArgumentException is thrownFileEditorCapable of resolving Strings to File-objectsLocaleEditorCapable 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 providesPropertiesEditorCapable of converting Strings (formatted using the format as defined in the Javadoc for the java.lang.Properties class) to Properties-objectsStringArrayPropertyEditorCapable of resolving a comma-delimited list of String to a String-array and vice versaURLEditorCapable 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),有时候这会派上用场。

上一篇: java.net.UnknownServiceException翻译
下一篇: AppFuse1.7 Tutorial 之 CreateManager (2)

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:java.net.SocketException翻译

下一篇:对极限编程四个核心的理解(二)

IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设

网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源

网站联盟: 联盟新闻 联盟介绍 联盟点评 网赚技巧

行业资讯: 搜索引擎 网络游戏 电子商务 广告传媒

网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它

服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护

软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷

网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash

程序设计: Java技术 C/C++ VB delphi

网络知识: 网络协议 网络安全 网络管理 组网方案 Cisco技术

操作系统: Win2000 WinXP Win2003 Mac OS Linux FreeBSD

热门词条
最新资讯
热门关注
热门标签