Parameter bindings in the SPRINGMVC

Source: Internet
Author: User

Parameter bindings in the SPRINGMVC

  Definition of parameter bindings

The so-called parameter binding, simply means that the client sends the request, and the request contains some data, then the data how to reach the Controller. Requests Key/value data from the client, such as the data contained in a GET request, and binds the Key/value data to the parameters of the Controller method. In Springmvc, the data submitted by the receiving page is received through a method parameter. Instead of the Controller class, define the member variable to receive.

  

  Types supported by default in SPRINGMVC

    

  To bind a custom parameter type

For some parameter types, parameter type binding is required because the type of the parameter that we enter differs from the parameter type in the entity class, so that the value of the pass is not successful. The following is an example of how to make a custom parameter type binding using the date type.

User.java

Importjava.util.Date; Public classUser {PrivateInteger ID; PrivateString username; PrivateString sex; PrivateDate birthday;  PublicInteger getId () {returnID; }      Public voidsetId (Integer id) { This. ID =ID; }      PublicString GetUserName () {returnusername; }      Public voidSetusername (String username) { This. Username = Username = =NULL?NULL: Username.trim (); }      PublicString Getsex () {returnsex; }      Public voidsetsex (String sex) { This. sex = Sex = =NULL?NULL: Sex.trim (); }      PublicDate Getbirthday () {returnbirthday; }      Public voidsetbirthday (Date birthday) { This. Birthday =birthday; }}

JSP page: Note the Name property value of the input box is consistent with the properties of the POJO entity class above to map successfully.

<form action= "Pojo" method= "POST" >         User id:<input type= "text" name= "id" value= "2" ></br>         user name:<input type= "text" name= "username" value= "Marry" ></br>         Gender:<input Type= "text" name= "Sex" value= "female" ></br>         date of birth:<input type= "text" name= "Birthday" value= " 2017-08-25 "></br>        <input type=" Submit "value=" Submission ">    </form>

Because the birthday we entered is of type string, but the birthday in the entity class is the date type, at this point, the binding succeeds and the corresponding controller is requested to receive the following error:

  

So we need to do parameter binding.

Parameter binding consists of two main steps:

1. Create a new class to implement the converter interface

Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;Importjava.util.Date;ImportOrg.springframework.core.convert.converter.Converter;//need to implement the converter interface, here is the conversion of string type to date type Public classDateConverterImplementsConverter<string, date>{@Override PublicDate Convert (String source) {//implement to convert a string to a date type (format is YYYY-MM-DD HH:mm:ss)SimpleDateFormat DateFormat =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Try {            returndateformat.parse (source); } Catch(ParseException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        //If parameter binding fails, returns null        return NULL; } }

  

2. Configure in the configuration file

<mvc:annotation-driven conversion-service= "Conversionservice" ></mvc:annotation-driven>          Class= "Org.springframework.format.support.FormattingConversionServiceFactoryBean" >        <property Name= "Converters" >            <!--Custom Converter's class name--Class            = "Com.ys.util.DateConverter" ></bean >        </property>    </bean>

This completes the binding for the custom parameter type.

Parameter bindings in the 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.