2009-04-29 144 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