1

I am new to Spring MVC and went thru some tutorials on net.

Came across below code in one of handler class under my project.

When form is submitted for action userHistory , call goes to my below handler method

@RequestMapping(value="/userHistory", method=RequestMethod.GET) public @ResponseBody UserDetails getUserHistory(Model model, HttpServletRequest request, HttpServletResponse response) { model.addAttribute("userDetail", new userDetail()); } 

Does DispatcherServlet construct empty Model object and pass to handler method getUserHistory?

Similarly when i submit for action "/userDetail" from my jsp, i get method parameter "userDetail" object filled with required data.

@RequestMapping(value="/userDetail", method=RequestMethod.POST) public String userDetail(UserDetail userDetail, HttpServletRequest request, HttpServletResponse response, Locale locale) {} 

Is DispatcherServlet doing this or some other interceptor?

UPDATE:- JSP code snippet is

 <form:form id="userForm" action="path/userDetail" method="post" commandName="userDetail"> 

1 Answer 1

2

Does DispatcherServlet construct empty Model object and pass to handler method getUserHistory?

Partially yes, an empty Model gets constructed, and its passed to the method getUserHistory. But its not really done by the DispatcherServlet, rather an implementation of HandlerMethodArgumentResolver (in the particular case a ModelMethodProcessor class). When the matching of the method is done, before the method is actually called another process takes place, that is argument resolving. The signature of the matched method is inspected, and objects of certain types known to spring get automatically resolved and injected by Spring. The types are defined in the docs, http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-methods check the list under subtitle Supported method argument types. All of the listed types, have a registered HandlerMethodArgumentResolver implementation that guides the creation of these objects

Is DispatcherServlet doing this or some other interceptor?

Picking up with the first answer, you can register your custom argument resolver. This great blog article tells you all you need to know to implement one. Note that UserDetail by what you have described could also be a form-backing bean, whose values are bound to the values of the input fields of the submitted form, here's an example http://www.codejava.net/frameworks/spring/spring-mvc-form-handling-tutorial-and-example

Sign up to request clarification or add additional context in comments.

3 Comments

Thanaks Master Slave. See my update. one doubt on this :- 1) I have mentioned neither @ModalAttribute nor written Custom Argument Resolver(just mentioned commandName ="userdDetail"), still i get the correct data under UserDetail param object of UserDetail() method. I believe these are resolved automatically while submission ?
posted a separate question for this stackoverflow.com/questions/27589520/…
sorry, I'm away from the keyboard, so harder to answer. Still, you've rightly noticed that you can ommit @ModelAttribute, in fact, having commandObject on the form will mark userDetail as a model attribute, and the same argument resolver process will kick-in, using this time ModelAttributeMethodProcessor. Its not bad that you've posted as a separate question though, I expect that the answers will kick in with greater detail

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.