From Spring 2.5 MVC to Spring 3.0 MVC

Hi guys, I am trying to learn Spring, and I am following a tutorial written in Spring 2.5. My research has shown me that SimpleFormController has depreciated in favor of the @Controller annotation. I am trying to convert this class to a controller class. Someone can show me how to do this under my class. I'm not sure about the methods in the class, but will they change or just add annotations to my class?

package springapp.web; import org.springframework.web.servlet.mvc.SimpleFormController; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.view.RedirectView; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import springapp.service.ProductManager; import springapp.service.PriceIncrease; public class PriceIncreaseFormController extends SimpleFormController { /** Logger for this class and subclasses */ protected final Log logger = LogFactory.getLog(getClass()); private ProductManager productManager; public ModelAndView onSubmit(Object command) throws ServletException { int increase = ((PriceIncrease) command).getPercentage(); logger.info("Increasing prices by " + increase + "%."); productManager.increasePrice(increase); logger.info("returning from PriceIncreaseForm view to " + getSuccessView()); return new ModelAndView(new RedirectView(getSuccessView())); } protected Object formBackingObject(HttpServletRequest request) throws ServletException { PriceIncrease priceIncrease = new PriceIncrease(); priceIncrease.setPercentage(20); return priceIncrease; } public void setProductManager(ProductManager productManager) { this.productManager = productManager; } public ProductManager getProductManager() { return productManager; } } 
+4
source share
2 answers

By annotating the createPriceIncrease method with @ModelAttribute , you tell spring how to initially populate the priceIncrease model value.

@SessionAttributes tells spring to automatically save the priceIncrease object in the session after each request.

Finally, @ModelAttribute in the method parameter for the "post" and "get" methods tells spring to find a model attribute called "priceIncrease".
He will know this session attribute and find it there, if possible, otherwise he will create it using the createPriceIncrease method.

 @Controller @SessionAttributes({"priceIncrease"}) @RequestMapping("/priceIncrease") public class MyController { @ModelAttribute("priceIncrease") public PriceIncrease createPriceIncrease() { PriceIncrease priceIncrease = new PriceIncrease(); priceIncrease.setPercentage(20); return priceIncrease; } @RequestMapping(method={RequestMethod.POST}) public ModelAndView post(@ModelAttribute("priceIncrease") PriceIncrease priceIncrease, HttpServletRequest request, HttpServletResponse response) { ... } @RequestMapping(method={RequestMethod.GET}) public ModelAndView get(@ModelAttribute("priceIncrease") PriceIncrease priceIncrease, HttpServletRequest request, HttpServletResponse response) { ... } } 
+2
source

The controller must not extend any class; just sign it accordingly.

I think "Bare Bones Spring" is a good 3.0 tutorial.

+1
source

All Articles