What is the purpose of binding binding in spring MVC

This is the internet code for init binder

@InitBinder public void initBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); } 

Can someone explain please:

1) Why this is used, I mean, what was the problem before, how it was solved using this function. so I want to know what was the problem with the original date that was resolved with this date format?

2) How to use this format in terms of the JSP form, I mean, if we enter the date in text format, it is converted to a specific format and then saves it?

3) How is this formatting applied, I mean, do I need to do something in the object class?

+59
java spring-mvc binding
Mar 06 2018-11-11T00:
source share
2 answers

1) Before that, you had to resort to manual date analysis:

  public void webmethod(@RequestParam("date") String strDate) { Date date = ... // manually parse the date } 

Now you can immediately get the parsed date:

  public void webmethod(@RequestParam("date") Date date) { } 

2) If your jsp page contains a date in the form yyyy-MM-dd , you can get it as a Date object directly in your controller.

3) Spring is trying against all registered editors to see if values ​​can be converted to objects. You do not need to do anything in the object itself, which is its beauty.

+52
Mar 06 2018-11-11T00:
source share

Spring automatically binds simple data (strings, int, float, etc.) into the properties of your bean command. However, what happens when the data is more complex, for example, what happens when you want to capture a string in the format "January 20, 1990" and create Spring to create a Date Object from it as part of the binding operation. For this to work, you need to tell Spring Web MVC to use PropertyEditor instances as part of the binding process:

 @InitBinder public void bindingPreparation(WebDataBinder binder) { DateFormat dateFormat = new SimpleDateFormat("MMM d, YYYY"); CustomDateEditor orderDateEditor = new CustomDateEditor(dateFormat, true); binder.registerCustomEditor(Date.class, orderDateEditor); } 
+10
Dec 08 '16 at 10:14
source share



All Articles