Reputation: 5654
I am trying to learn Spring and I am following a tutorial t ihats written in spring 2.5. My research has shown me that the SimpleFormController has been depreciated in favour of the annotation @Controller. I am trying to convert this class into a Controller Class can someone show me how this is done, under is my class. I am not sure about the methods in the class but will those also change or do i 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;
}
}
Upvotes: 3
Views: 2579
Reputation: 11805
By annotating the "createPriceIncrease" method with @ModelAttribute
, you're telling spring how to initially populate the "priceIncrease" model value.
The @SessionAttributes
tells Spring to automatically store the "priceIncrease" object in session after each request.
Finally the @ModelAttribute
on the method parameter for the "post" and "get" methods tells spring to find a model attribute named "priceIncrease".
It will know it's a session attribute, and find it there if it can, otherwise, it 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) {
...
}
}
Upvotes: 2
Reputation: 308743
Controller need not extend any class; just annotate it appropriately.
I think "Bare Bones Spring" is a good 3.0 tutorial.
Upvotes: 1