{"id":2598,"date":"2018-01-19T14:35:02","date_gmt":"2018-01-19T14:35:02","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=2598"},"modified":"2019-04-08T12:49:19","modified_gmt":"2019-04-08T12:49:19","slug":"spring-mvc-init-binder","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/spring-mvc-init-binder\/","title":{"rendered":"Spring MVC &#8211; init() binder"},"content":{"rendered":"<p>To customize request parameter data binding, we can use @InitBinder annotated methods within our controller.<\/p>\n<ol>\n<li>The @InitBinder annotated methods will get called on each HTTP request if we don&#8217;t specify the &#8216;value&#8217; element of this annotation.<\/li>\n<li>Each time this method is called a new instance of WebDataBinder is passed to it.<\/li>\n<li>To be more specific about which objects our InitBinder method applies to, we can supply &#8216;value&#8217; element of the annotation @InitBinder. The &#8216;value&#8217; element is a single or multiple names of command\/form attributes and\/or request parameters that this init-binder method is supposed to apply to.<\/li>\n<\/ol>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n    @InitBinder(&quot;User&quot;)\r\n    public void customizeBinding (WebDataBinder binder) \r\n    {\r\n      .\r\n      .\r\n      .\r\n    }\r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\r\n@Controller\r\n@RequestMapping(&quot;\/register&quot;)\r\npublic class UserRegistrationController \r\n{\r\n    @InitBinder(&quot;user&quot;)\r\n    public void customizeBinding (WebDataBinder binder) \r\n    {\r\n        SimpleDateFormat dateFormatter = new SimpleDateFormat(&quot;yyyy-MM-dd&quot;);\r\n        dateFormatter.setLenient(false);\r\n        binder.registerCustomEditor(Date.class, &quot;dateOfBirth&quot;, new CustomDateEditor(dateFormatter, true));\r\n    }\r\n    \r\n    .\r\n    . \r\n}\r\n<\/pre>\n<p><em>simpleDateFormat.setLenient(false); &#8212; Will check for out of range Date. For Example, If lenient is set to true, Aug 32 will be converted to Sep 1 .<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>To customize request parameter data binding, we can use @InitBinder annotated methods within our controller. The @InitBinder annotated methods will get called on each HTTP request if we don&#8217;t specify the &#8216;value&#8217; element of this annotation. Each time this method is called a new instance of WebDataBinder is passed to it. To be more specific&hellip; <a href=\"https:\/\/codethataint.com\/blog\/spring-mvc-init-binder\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[273],"tags":[],"class_list":["post-2598","post","type-post","status-publish","format-standard","hentry","category-spring-mvc"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2598","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=2598"}],"version-history":[{"count":1,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2598\/revisions"}],"predecessor-version":[{"id":2599,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2598\/revisions\/2599"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=2598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=2598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=2598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}