Friday, September 22, 2017

Some Spring MVC-Javascript Notes

  1. When there's a page locked to a session and one would like the page to detach from the session if the user navigates to some other page, this can be done by Javascript. JS's 'onunload' event comes pretty hand in situations like these.
  2. Let's say you make a post request with a modelattribute inside a form. The modelattribute fails to bind because of JavaBeans constraint annotations on the Java entity definition class. This could happen for several reasons. For eg, let's say you had a constraint on the length of the name and the supplied length was smaller than required. The binding shall fail and the BindingResult object shall have the errors. Now, you don't have to reattach the modelattribute to the ModelMap object. All you have to do is to return the String in the requestMapping method and have necessary Spring-error fields ready to display the related binding error.
  3. There has been this confusion about BindingResult and @InitBinder for quite some time now. In short, modelAttributes bind automatically with the Java object, as long as the fields within are of primary types. When there's a user-defined or complicated object field inside modelattribute class, to help them bind with the received modelAttribute, you need @InitBinder definition, for each one of the complex fields. BindingResult will simply hold all that went wrong while trying to bind the received modelAttribute into a Java object.

No comments:

Post a Comment