Annotations in Spring MVC

I would like to convert this SimpleFormController to use annotation support introduced in Spring MVC 2.5

Java

public class PriceIncreaseFormController extends SimpleFormController { ProductManager productManager = new ProductManager(); @Override public ModelAndView onSubmit(Object command) throws ServletException { int increase = ((PriceIncrease) command).getPercentage(); productManager.increasePrice(increase); return new ModelAndView(new RedirectView(getSuccessView())); } @Override protected Object formBackingObject(HttpServletRequest request) throws ServletException { PriceIncrease priceIncrease = new PriceIncrease(); priceIncrease.setPercentage(20); return priceIncrease; } } 

Spring Configuration

 <!-- Include basic annotation support --> <context:annotation-config/> <!-- Comma-separated list of packages to search for annotated controllers. Append '.*' to search all sub-packages --> <context:component-scan base-package="springapp.web"/> <!-- Enables use of annotations on controller methods to map URLs to methods and request params to method arguments --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <bean name="/priceincrease.htm" class="springapp.web.PriceIncreaseFormController"> <property name="sessionForm" value="true"/> <property name="commandName" value="priceIncrease"/> <property name="commandClass" value="springapp.service.PriceIncrease"/> <property name="validator"> <bean class="springapp.service.PriceIncreaseValidator"/> </property> <property name="formView" value="priceincrease"/> <property name="successView" value="hello.htm"/> <property name="productManager" ref="productManager"/> </bean> 

Basically, I would like to replace the whole XML configuration for /priceincrease.htm bean with annotations in the Java class. Is this possible, and if so, what are the relevant annotations I should use?

Thanks Don

+4
source share
2 answers

It will look something like this: although it works or is not entirely accurate, it will depend a little on your configuration (view resolver, etc.). I should also note that there are about eight billion valid ways to write this thing. See Spring Documentation 13.11.4 “Supported Handler Method Arguments and Return Types” for an overview of insanity. Also note that you can authorize the validator

 @Controller @RequestMapping("/priceincrease.htm") public class PriceIncreaseFormController { ProductManager productManager; @Autowired public PriceIncreaseFormController(ProductManager productManager) { this.productManager = productManager; } // note: this method does not have to be called onSubmit @RequestMapping(method = RequestMethod.POST) public String onSubmit(@ModelAttribute("priceIncrease") PriceIncrease priceIncrease, BindingResult result, SessionStatus status { new PriceIncreaseValidator().validate(priceIncrease, result); if (result.hasErrors()) { return "priceincrease"; } else { int increase = priceIncrease.getPercentage(); productManager.increasePrice(increase); status.setComplete(); return "redirect:hello.htm"; } } // note: this method does not have to be called setupForm @RequestMapping(method = RequestMethod.GET) public String setupForm(Model model) { PriceIncrease priceIncrease = new PriceIncrease(); priceIncrease.setPercentage(20); model.addAttribute("priceIncrease", priceIncrease); return "priceincrease"; } } 
+6
source

Someone finished this project with recent MVC, and it's on github, so you can see how all the classes have changed compared to the Spring tutorial.

Link: PriceIncreaseFormController

0
source

All Articles