简体   繁体   中英

From Spring 2.5 MVC to Spring 3.0 MVC

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;
    }
    
    

}

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) {
     ...
  }

}

Controller need not extend any class; just annotate it appropriately.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM