概述
Engineering
Keith Donald
November 17, 2009
The Spring 3 final release is right around the corner, and it’s going to be a great release. In this blog entry, I will take you through some of Spring 3’s type conversion and validation enhancements. Whether you are developing a traditional web application, a desktop application, or a “next-generation” RIA, data binding, type conversion, and validation are important areas. As you’ll see in this entry, Spring 3 gives you a significant upgrade in each of these areas while preserving backwards compatibility with previous releases.
New System Goals
Before I get into features, I’d first like to highlight the goals we had when we set out to improve Spring 3’s data binding system:
Provide a stateless, strongly-typed Type Converter SPI that supercedes JavaBean PropertyEditors
Provide a unified Type Conversion API to use anywhere conversion is required, including Spring’s DataBinder and Expression Language
Allow for type conversion to be driven by Java Annotation metadata
Simplify by registering sensible defaults and applying convention-over-configuration
As Spring 3 final approaches, I believe we have delivered on each of the goals. Read on and you be the judge of that.
Features
The first environment that takes full advantage of the new type conversion system is Spring MVC, which I’ll pull from to demonstrate the new features. This starts with the new Spring MVC 3 configuration namespace:
<mvc:annotation-driven />
The preceding minimal configuration causes Spring to automatically install default type converters that localize Number and Date fields, including full support for the popular Joda Time library if it is present on your classpath. In addition, Spring automatically enables annotation-driven declarative validation if a JSR-303 provider, such as Hibernate Validator, is present on your classpath.
Now when you bind to a field, that field will be printed and parsed for the user’s locale using a sensible default format. To illustrate, consider the following model:
public class Account {
private Date activationDate = new Date(1258466400);
private BigDecimal balance = new BigDecimal("3000.25");
}
… printed on a form for users in the US and DE Locales:
Locale.US Locale.DE
Activation Date: Balance:
Activation Date: Balance:
Overriding Defaults with Annotations
Often you need to format a field in a manner that differs from the default format. In the previous example, you probably want the activationDate field to be formatted using a short date format, and the balance field to be formatted using a currency format. In previous versions of Spring, you would register a custom PropertyEditor in your Controller to achieve this. In Spring 3, you simply annotate your fields:
private class Account {
@DateTimeFormat(style="S-")
private Date activationDate = new Date(1258466400);
@NumberFormat(style=Style.CURRENCY)
private BigDecimal balance = new BigDecimal("3000.25");
}
With the annotation overrides, the following is printed for the US and DE Locales:
Locale.US Locale.DE
Activation Date: Balance:
Activation Date: Balance:
Parameter Annotations
Annotation-driven overrides can also be applied to method parameters. Consider the following Controller method that fetches upcoming appointments for a specific day, where the day is a URL path variable encoded in ISO date format:
@RequestMapping(value = "/appointments/{day}", method = RequestMethod.GET)
public String getAppointmentsForDay(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date day) {
...
}
Sending a GET request to /appointments/2009-11-17 fetches all Appointments on November 17th, 2009. Setting the @DateTimeFormat iso attribute to ISO.DATE instructs Spring to parse the incoming date string as an ISO Date (yyyy-mm-dd).
Validation
It is common to validate a model after binding user input to it. Spring 3 provides support for declarative validation with JSR-303. This support is enabled automatically if a JSR-303 provider, such as Hibernate Validator, is present on your classpath. When enabled, you can trigger validation simply by annotating a Controller method parameter with the @Valid annotation:
@RequestMapping(value = "/appointments", method = RequestMethod.POST)
public String add(@Valid AppointmentForm form, BindingResult result) {
....
}
static class AppointmentForm {
@NotNull @Future
private Date date;
}
After binding incoming POST parameters, the AppointmentForm will be validated; in this case, to verify the date field value is not null and occurs in the future.
Convention Over Configuration
Finally, consider how the principle of convention over configuration can be applied to type conversion. In a business application, you will often define your own custom field types. In previous versions of Spring, to format such types you would create a custom PropertyEditor implementation and register it in your Controller. With Spring 3, in most cases you simply can adhere to the following convention:
Define a static valueOf(String) method or Constructor(String) to parse your value from its String representation
Implement toString() to print your value for display
Consider a SocialSecurityNumber type that adheres to this convention:
public class SocialSecurityNumber {
@Size(9)
@Mask("###-##-####")
private String value;
public SocialSecurityNumber(String value) {
this.value = value;
}
public String toString() {
return this.value;
}
}
When a SocialSecurityNumber field is printed for display, toString() will be called; when a client value is parsed, the Constructor will be called. No separate Formatter or PropertyEditor implementation is required.
Summary
This entry has covered some of the new Spring 3 type conversion and validation features. To learn more, including how to implement your own type converters, checkout the Spring 3 Reference Guide. Also, watch for the release of the revised Petclinic 3 sample application in the coming weeks (this sample app demonstrates all the features highlighted here and is currently available for early access in our SVN repo here) .
I am excited about the foundation these new capabilities provide us moving forward! Please do let me know about your experiences applying and extending these features, and keep the feedback and ideas coming at jira.springframework.org.
comments powered by Disqus
translate:
翻译:
Spring3的最终版本即将发布,它将是一个很棒的版本。在这篇博客文章中,我将介绍Spring3的一些类型转换和验证增强。无论是开发传统的web应用程序、桌面应用程序还是“下一代”RIA,数据绑定、类型转换和验证都是重要的领域。正如您将在本文中看到的,Spring 3为您提供了这些方面的重大升级,同时保留了与以前版本的向后兼容性。
新系统目标
在进入特性之前,我首先要强调一下我们在着手改进Spring3数据绑定系统时的目标:
提供一个无状态的、强类型的类型转换器SPI,取代JavaBean属性编辑器
提供一个统一的类型转换API,以便在任何需要转换的地方使用,包括Spring的DataBinder和Expression语言
允许由Java注释元数据驱动类型转换
通过注册合理的默认值和对配置应用约定来简化
随着Spring 3的最后一次临近,我相信我们已经实现了每一个目标。继续读下去,你就是这个的评判者。
最后
以上就是孤独咖啡豆为你收集整理的Spring 3 Type Conversion and Validation的全部内容,希望文章能够帮你解决Spring 3 Type Conversion and Validation所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复