Spring MVC - Automatically populate an object from a form view?

In ASP.NET MVC in the controller, I can just have an object from my model as a parameter in one of my methods, and the form submission that is processed by this method will automatically fill the object.

eg:

[AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(User u){...} 

The custom object will be automatically populated for submission from the form.

Is there a way to do this automatically using Spring MVC, and if so, how to do it?

+4
source share
3 answers

In Spring MVC (with Spring MVC 2.5+ annotation based configuration), it looks exactly the same:

 @RequestMapping(method = RequestMethod.POST) public ModelAndView edit(User u) { ... } 

The User object will be automatically populated. You can also explicitly specify the name of the corresponding model attribute using the @ModelAttribute annotation (by default, the attribute name is the name of the argument class with the decapitalized first letter, that is, "user")

 ... (@ModelAttrbiute("u") User u) ... 
+3
source

http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/web/portlet/mvc/SimpleFormController.html#onSubmitAction(java.lang.Object)

Create a form controller like PriceIncreaseFormController and make it extend SimpleFormController

to override the public ModelAndView onSubmit(Object command) method there are many variations of the above. Look for a suitable method that suits your needs. For a simple stream, the above method should be sufficient.

Inside the method, you can execute the typecast command and get your Command class.

 commandObj = ((PriceIncrease) command) 

commandObj will have parameters populated by spring.

in your springapp-servlet.xml you should tell spring about the PriceIncrease command class as follows, and you must also have a POJO for the class of commands created.

 <bean name="/priceincrease.htm" class="springapp.web.PriceIncreaseFormController"> <property name="commandClass" value="springapp.service.PriceIncrease"/> 

....

+1
source

Not in servlets, but absolutely in Spring MVC. Take a look at the web framework .

In particular, Section 13.11.4 , 9th marker.

0
source

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


All Articles