3

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

}
4

2 に答える 2

2

「createPriceIncrease」メソッドに で注釈を付けることで、@ModelAttributeSpring に「priceIncrease」モデル値を最初に入力する方法を伝えます。

@SessionAttributes、各リクエストの後に「priceIncrease」オブジェクトをセッションに自動的に保存するよう Spring に指示します。

最後に@ModelAttribute、「post」メソッドと「get」メソッドのメソッド パラメータで、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) {
     ...
  }

}
于 2012-06-25T03:27:07.223 に答える
1

コントローラーはクラスを拡張する必要はありません。適切に注釈を付けるだけです。

「Bare Bones Spring」は 3.0 の良いチュートリアルだと思います。

于 2012-06-24T23:45:57.673 に答える