Initialization parameter bindings for SPRINGMVC

Source: Internet
Author: User

Initialize parameter bindings: Date format

A: First we do a date format binding, configuration initialization parameter binding and custom type conversion have the same wonderful

The configuration steps are as follows:

1. We first configure the Applicationcontext.xml for scanner configuration

 

2. Next we define its processor, and the binding of the method (@InitBinder)

3. Test in a single date format:

<form action= "${pagecontext.request.contextpath}/first.do" method= "POST" >   

Two: multiple date formats

1. First we need to inherit an editor (Propertieseditor)

Rewrite it's Setastext method

So that subsequent assignments to the Date object

public class Mydateeditor extends propertieseditor{    @Override public    void Setastext (String source) throws illegalargumentexception {      SimpleDateFormat sdf=getdate (source);      try {setValue (Sdf.parseobject (source)),      } catch (ParseException e) {e.printstacktrace ();      }         } Private SimpleDateFormat getDate (String source) {SimpleDateFormat sdf=null;if (pattern.matches ("^\\d{4}-\\d{2}-\\d{2 }$ ", source)) {sdf=new SimpleDateFormat (" Yyyy-mm-dd ");} else if (pattern.matches ("^\\d{4}/\\d{2}/\\d{2}$", source)) {sdf=new SimpleDateFormat ("Yyyy/mm/dd");} else if (pattern.matches ("^\\d{4}\\d{2}\\d{2}$", source)) {sdf=new SimpleDateFormat ("YyyyMMdd");} else {throw new Typemismatchexception ("", Date.class);} return SDF;}}

2. Configuring the Processor

3. Perform a variety of date format tests:

Now belongs to your own not always belong to you, initialization parameters will also change with the binding.

----warned yourself

Initialization parameter bindings for SPRINGMVC

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.