Data binding without using spring taglib

My html is built without using taglib spring, and now I want to bind the form parameters to an object in my controller.

Currently my form looks like

<form> <input type="text" name="frAccUserMgmt.userName"/> <input type="password" name="frAccUserMgmt.userPwd"/> </form> 

The corresponding part of my object

 Class FrAccUserMgmt { private String userName; private Strint userPwd; // getter and setter } 

My controller

 @RequestMapping("login") Public ModelAndView doLogin(FrAccUserMgmt frAccUserMgmt) { //code } 

How do I snap it. No bindings are currently in progress. I just get empty code in my code.

+3
source share
2 answers

You can try to include the BindingResult class in the method signature, and then see if there are any binding errors:

 @RequestMapping("login") Public ModelAndView doLogin(FrAccUserMgmt frAccUserMgmt, BindingResult result) { if (result.hasErrors()) { logger.warn("BindingResult errors: " + result.toString()); } //code } 

Remove the frAccUserMgmt part from the form field names. Spring will automatically find the command object to bind the query parameters based on the getters and setters defined in the command object.

+1
source

You can also do this by adding the @ModelAttribute parameter to the bean parameter, which must be populated with query parameters.

According to spring docs

http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib-method-args (16.3.3.8 Using the @ModelAttribute attribute in a method argument )

An @ModelAttribute in the method argument indicates that the argument should be retrieved from the model. If not in the model, the argument must first be created and then added to the model. After being present in the model, the argument fields must be filled out from all query parameters that have the corresponding names. This is known as data binding in spring MVC, a very useful mechanism that eliminates the need to analyze each form field separately.

0
source

Source: https://habr.com/ru/post/1341309/


All Articles