2009-04-29 199 views
4

我想這個SimpleFormController轉換爲使用Spring MVC的2.5註釋在Spring MVC

的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配置

<!-- 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> 

的註解支持基本上,我想替換的所有XML配置豆與Java類中的註釋。這是否可能,如果是這樣,我應該使用哪些相應的註釋?

謝謝, 唐

+0

而你的問題是......? – 2009-04-29 18:06:19

回答

6

它會看起來像下面,但它是否有效或不完全按原樣將取決於您的配置(視圖解析器等)位。我還應該注意到,有大約八十億個有效的方法來寫這個東西。請參閱Spring文檔13.11.4 "Supported handler method arguments and return types"以瞭解精神錯亂的概述。還要注意的是,你可以自動裝配驗證

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

} 
+1

這就是我鄙視的80億有效方法,我通常直接實現Controller - 至少可以根據需要跟蹤接口。註釋和表單控制器層次結構都讓IMO感到困惑。 – MetroidFan2002 2009-04-30 16:52:11

0

有人完成了這個項目,最近的MVC和它在GitHub上,這樣你就可以看到所有的類是如何比較Spring的教程改變。

鏈接:PriceIncreaseFormController