SPRINGMVC Date Data Type binding

Source: Internet
Author: User

Data binding should be one of the features of spring MVC ~ Easy to use and powerful, greatly simplifying the way our programmers receive and convert user input data.

Data binding in previous versions of Spring is completely propertyeditor based. In Spring3, Converter is introduced to replace the PropertyEditor completion type conversion.

So we're going to do this in this order, first of all, to implement date-type data binding based on traditional propertyeditor.

In spring MVC, we can register custom PropertyEditor with Webdatabinder to add the corresponding request parameter bindings. There are two ways of doing this:

1. Use Method 2, custom Webbindinginitializer in @initbinder annotation @controller to provide a global data binding rule.

1. Using @initbinder annotations

@InitBinder    Public void Initbinder (Webdatabinder binder) {      binder.registercustomeditor (Date. class New dateeditor ());  }  
 Public classDateeditorextendsPropertyEditorSupport {@Override Public voidSetastext (String text)throwsillegalargumentexception {simpledateformat format=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Date Date=NULL; Try{Date=format.parse (text); } Catch(parseexception e) {format=NewSimpleDateFormat ("Yyyy-mm-dd"); Try{Date=format.parse (text); } Catch(ParseException E1) {e1.printstacktrace ();      }} setValue (date); }  }  

Here we will dateeditor proposed to encapsulate into a class for easy reuse.

In addition here is a try...catch small magical, is first to "Yyyy-mm-dd HH:mm:ss" form to parse the user input parameters, if the resolution fails to "YYYY-MM-DD" form to parse. This logic can simultaneously deal with "Yyyy-mm-dd HH:mm:ss" and "yyyy-mm-dd" form of date data, I think in the general Chinese system, these two forms should be the most common.

After adding the above code, @InitBinder controller can automatically bind the date type data, but this is only in effect in the controller, if you want to take effect in the global scope, The controller that contains the @initbinder annotation can be defined as a basecontroller, and the rest controller inherits the controller. Of course there is another way, if you are interested, please see 2.

2. Custom Webbindinginitializer

 Public class Implements Webbindinginitializer {            @Override      publicvoid  Initbinder (webdatabinder Binder, WebRequest request) {          binder.registercustomeditor (Date. class New dateeditor ());      }  }  

Or in front of the dateeditor, so quickly meet again, but the location of the registration changed, in the Webbindinginitializer registered PropertyEditor is in the global scope of sharing.

But the light is not enough, but also to inject webbindinginitializer into the annotationmethodhandleradapter.

    <Beanclass= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">        < Propertyname= "Webbindinginitializer">            <Beanclass= "com.**.**.**." Mywebbindinginitializer " />        </ Property>    </Bean>    

The perfect solution to the problem

Test code

@Controller @requestmapping (Value= "/")  Public class Basecontroller {        @RequestMapping (value= "index")    public  String indexjsp ( String username,date date,date ddate) {        System.out.println (username);        SYSTEM.OUT.PRINTLN (date);        System.out.println (ddate);         return "Index";}    }

Test parameters

index?username=fasdf&date=2012-12-03&ddate=2013-03-06

Output Result:

00:00:00 CST00:00:00 CST 2013

Reference Document: http://blog.csdn.net/u012345283/article/details/43268081

SPRINGMVC Date Data Type binding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.