博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Framework’s WebDataBinder
阅读量:5759 次
发布时间:2019-06-18

本文共 9230 字,大约阅读时间需要 30 分钟。

 

Last week, I was just outside our nation’s capital teaching Spring Web MVC Framework to a wonderful group of people working for the National Institute of Health (NIH).  They are getting ready to move a collection of Struts 1 projects to Spring Web MVC.  Some questions and discoveries around Spring Web MVC’s @InitBinder operations seemed a good fit for this week’s post.

Spring Web MVC Command Beans

Part of my Spring MVC class is dedicated to teaching students how to have Spring bind data submitted by HTML form or query string parameters to a Java object (otherwise known as a Spring command bean or backing bean under this circumstance).

On the server, the data must be extracted out of the HttpServletRequest object, converted to a proper data format, loaded into an object, and validated. This can be done in code by the developer, but Spring comes with mechanisms to do this work for you automatically. The data, once in an object can be passed to the backend business components for appropriate processing.

In order to have Spring collect and bind the data from an HTML form page (or query string parameter data), just create a plain old Java object/JavaBean with properties that match the request parameter names. For example, if collecting order information from an HTML form like that below…

… you would need a class like Order shown here.

Then a Spring controller automatically will bind request data to the properties in an instance of Order and pass it to the controller handler method.  The addOrder( ) handler method shown here demonstrates how to write a handler method with a Command bean parameter.

 

Spring Web MVC InitBinder

Spring automatically binds simple data (Strings, int, float, etc.) into properties of your command bean.  However, what happens when the data is more complex?  For example, what happens when you want to capture a String in “Jan 20, 1990” format and have Spring create a Date object from it as part of the binding operation.  Perhaps you have custom types you want created from their string representation.  For example, you want Spring to take a String in ###-###-#### format and populate a PhoneNumber type property you have in your Command bean.

For this work, you need to inform Spring Web MVC to use PropertyEditor instances as part of the binding process.  The JavaBean API defines a java.beans.PropertyEditor interface. This interface defines methods to convert a property’s value to a String (getAsText()), and to set a property given a String (setAsText(String)). In particular, Spring Web MVC converts incoming request String data to the appropriate data type using PropertyEditors for the fields of the command beans.

Some PropertyEditors are provided and used by Spring Web MVC by default (this is how simple data types are handled).  How do you specify custom property editors for your command beans for the more complex typed fields?  In a method of the controller annotated with @InitBinder, you register and configure your custom PropertyEditors for Spring Web MVC to use at the time of data binding.  An example @InitBinder method is shown below to add a customized Date PropertyEditor to accept date strings in the format of “Jan 20, 1990” and convert these strings to java.util.Date objects for the Order object’s fields.  Note, the name of the annotated method is arbitrary.  The important part of this controller method is the @InitBinder annotation on the method.

Note also the WebDataBinder (a subclass of the more generic ) parameter of  the @InitBinder method.  This object manages the Spring binding process and holds a registry of the PropertyEditors used in the binding operation.In this example, Order’s orderDate and shipDate can be captured in “MMM d, YYYY” string format and be bound to java.util.Date objects by Spring.

Handling Two Properties of the Same Type

A problem arises when the binding operation must deal with two properties of the same type and String data for those properties is formatted differently.  This is the problem three of my students (Nick, Erik and Nivedita) worked on and solved in last week (thanks guys!).

For example, if you note, there are two Date properties in the Order type above.  Currently, the WebDataBinder has been given just one CustomDateEditor that uses the “MMM d, YYYY” format for accepting date Strings into both the shipDate and orderDate properties.  So, under this design, all Date data would have to be provided in this format by the user in order for it to be accepted by Spring Web MVC for Orders.  What if you have two different formats for Dates?  For example, what if order dates are to be provided in “d-MM-yyyy” format and ship dates are to be provided in “MMM d, YYYY” format?

As Nick, Erik and Nivedita discovered, there is an overloaded registerCustomEditor( ) method in the DataBinder class.  The registerCustomEditor( ) calls in the @InitBinder method above pass in the target class type and the PropertyEditor instance to be used to perform the binding of that type of property.  Another registerCustomEditor method call allows you to specify the command bean field to which the custom PropertyEditor applies.  This allows you to set up PropertyEditors per property/field versus per general property data type.  Below, the @InitBinder method now contains the registration of two CustomDateEditors.  One used for orderDates (in d-MM-yyyy” format) while the other is used for shipment dates (in “MMM d, YYYY”) format.

 

Wrap Up

My thanks again to the folks at NIH and Nick, Eric, and Nivedita in particular for asking questions about this feature and digging into the DataBinder details.  If you are looking to learn Spring, please consider taking one of our .  Our class materials were just updated to cover Spring Framework 4.  So come learn the latest and greatest about Spring in our fantastic learning environment.

https://www.intertech.com/Blog/spring-frameworks-webdatabinder/

 

转载于:https://www.cnblogs.com/softidea/p/10197701.html

你可能感兴趣的文章
自动找位置
查看>>
Scenario 9-Shared Uplink Set with Active/Active uplink,802.3ad(LACP)-Flex-10
查看>>
UML类图中的六种关系
查看>>
生产者消费者问题理解与Java实现
查看>>
【CentOS 7架构21】,Nginx的安装#180104
查看>>
探寻Interpolator源码,自定义插值器
查看>>
一致性哈希
查看>>
mysql(待整理)
查看>>
看雪论坛502,出现安全宝?
查看>>
华为交换机隐藏配置模式
查看>>
修改git环境默认路径 (通过设置home环境变量来设置)
查看>>
springSSM 使用poi导出excel(一)
查看>>
Json(Json-lib)中使用JSONObject.toBean(JSONObject jsonObject, Class beanClass)日期保存了当前时间...
查看>>
我的友情链接
查看>>
基于 Docker 的微服务架构实践
查看>>
TPYBoard超全DIY案例一览:轻松玩转MicroPython开发!
查看>>
Playfair密码算法Java实现
查看>>
Java学习笔记(2015.7.27~7.31)
查看>>
(二)搭建容器云管理平台笔记—安装容器化环境
查看>>
Linux命令--积累
查看>>