繁体   English   中英

从 Spring 2.5 MVC 到 Spring 3.0 MVC

[英]From Spring 2.5 MVC to Spring 3.0 MVC

我正在尝试学习 Spring,并且正在学习 spring 2.5 中编写的教程。 我的研究表明,SimpleFormController 已贬值,取而代之的是注释 @Controller。 我正在尝试将此类转换为控制器类,谁能告诉我这是如何完成的,下面是我的课程。 我不确定类中的方法,但这些方法是否也会更改,或者我只是向我的类添加注释?

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

}

通过使用@ModelAttribute注释“createPriceIncrease”方法,您告诉 spring 如何初始填充“priceIncrease”模型值。

@SessionAttributes告诉 Spring 在每次请求后自动将“priceIncrease”对象存储在会话中。

最后,“post”和“get”方法的方法参数上的@ModelAttribute告诉 spring 找到一个名为“priceIncrease”的模型属性。
它会知道它是一个会话属性,如果可以的话会在那里找到它,否则,它将使用“createPriceIncrease”方法创建它。

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

}

控制器不需要扩展任何类; 只需对其进行适当的注释即可。

我认为“Bare Bones Spring”是一个很好的 3.0 教程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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