【问题标题】:annotations in Spring MVCSpring MVC 中的注解
【发布时间】:2010-10-22 15:43:52
【问题描述】:

我想将此 SimpleFormController 转换为使用 Spring MVC 2.5 中引入的注解支持

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

}

弹簧配置

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

基本上,我想用 Java 类中的注释替换 /priceincrease.htm bean 的所有 XML 配置。这可能吗,如果可以,我应该使用哪些相应的注释?

谢谢, 唐

【问题讨论】:

  • 你的问题是......?

标签: java spring spring-mvc annotations


【解决方案1】:

它看起来像下面这样,尽管它是否能完全按原样工作取决于您的配置(查看解析器等)。我还应该注意到,大约有 80 亿种有效的方法来编写这个东西。请参阅 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";
    }

}

【讨论】:

  • 我鄙视的是那八十亿种有效的方式,我通常只是直接实现Controller——至少这样我可以适当地跟踪接口。注释和表单控制器层次结构都使 IMO 感到困惑。
  • 对我来说关键是基于注释的控制器可以比使用旧类层次结构的控制器更容易进行单元测试。当然,适应它需要时间。我不想再错过的另一个功能是扩展方法参数注入的能力,但测试对我来说是最讨厌的。
【解决方案2】:

有人用最近的 MVC 完成了这个项目,它在 github 上,所以你可以看到所有类与 Spring 的教程相比是如何变化的。

链接:PriceIncreaseFormController

【讨论】:

    猜你喜欢
    • 2011-07-09
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    • 2011-04-08
    • 1970-01-01
    • 2018-03-12
    相关资源
    最近更新 更多