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