java Spring MVC 中的注解

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/803495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:52:26  来源:igfitidea点击:

annotations in Spring MVC

javaspringspring-mvcannotations

提问by Dónal

I'd like to convert this SimpleFormController to use the annotation support introduced in Spring MVC 2.5

我想将此 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;
    }

}

Spring Config

弹簧配置

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

Basically, I'd like to replace all the XML configuration for the /priceincrease.htmbean with annotations within the Java class. Is this possible, and if so, what are the corresponding annotations that I should use?

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

Thanks, Don

谢谢,唐

回答by Boden

It'll look something like the following, although whether it works or not exactly as is will depend a bit on your configuration (view resolver, etc). I should also note that there are about eight billion valid ways to write this thing. See the Spring documentation, 13.11.4 "Supported handler method arguments and return types"for an overview of the insanity. Also note that you can autowire the validator

它看起来像下面这样,尽管它是否完全按原样工作将取决于您的配置(查看解析器等)。我还应该注意到,大约有 80 亿种有效的方式来编写这个东西。有关疯狂的概述,请参阅 Spring 文档13.11.4“支持的处理程序方法参数和返回类型”。另请注意,您可以自动装配验证器

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

}

回答by George

Someone completed this project with a recent MVC and it's on github, so you can see how all the classes are changed compared to Spring's tutorial.

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

Link: PriceIncreaseFormController

链接:PriceIncreaseFormController