Parameter formatting in SPRINGMVC

Source: Internet
Author: User

The data editor in SPRINGMVC is primarily used to convert data types during data transfer, for example: Our front end has a "2015-04-28" date-formatted string, and we pass it directly into the date type that we need, which is required after the property editor.

Here we still use the date type conversion as an example:

Import Package Required: Joda-time-2.1.jar

index.jsp

<form action= "date" method= "POST" >
Date:<input type= "text" id= "date" name= "date"/><br/>
<input type= "Submit" value= "date"/>
</form>

Controller

@InitBinder
protected void Initbinder (Webdatabinder wdb)
{
SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd");
Wdb.registercustomeditor (date.class,new customdateeditor (sdf,true));
}

@RequestMapping ("/date")
Public String Date (@RequestParam ("date") Date date)
{
SYSTEM.OUT.PRINTLN (date);
Return "";
}


When we click on the button to send a string in the input yyyy-mm-dd format, it is recognized and then the parameter is annotated with the parameter of the date type, it is possible to implement the conversion: public string date (@DateTimeFormat ( Pattern= "Yyyy-mm-dd") @RequestParam ("date") Date date). The @InitBinder is an initialization editor binding that binds our specified editor to the controller, so @initbinder is only valid in the current controller. by Registercustomeditor registering a date editor of our own definition, SPRINGMVC in the process of passing parameters, the default is not to recognize the YYYY-MM-DD format of the data, so the above way to achieve the conversion of the parameter type.

In fact, the conversion of the date format in SPRINGMVC provides a very simple way to do that is to use the @datetimeformat annotation, annotate the annotation to the parameters that take the parameter before it, and then specify the value of its property pattern, "Yyyy-mm-dd" or "yyyy/mm /dd "to identify the parameters passed by the front end.

Here is the process of @requestparam (value= ""): When the argument is passed to try to convert the string to the type we need (for example, int, long), if it can be converted (SPRINGMVC built-in), the conversion succeeds, otherwise it will be an error.

Rich parameter formatting is provided in Springmvc, Datetimeformetter Numberformetter can refer to

Http://www.cnblogs.com/liukemng/p/3748137.html

Therefore, for some special conversions our master needs to use annotations in the parameters and to develop pattern.





Parameter formatting in 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.