java - Spring MVC 中的注解

标签 java spring spring-mvc annotations

我想将此 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 配置

<!-- 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 配置。这可能吗?如果可能,我应该使用哪些相应的注释?

谢谢, 唐

最佳答案

它看起来像下面这样,尽管它是否完全按原样工作将取决于您的配置( View 解析器等)。我还应该注意到,大约有 80 亿种有效的方式来编写这个东西。请参阅 Spring 文档,13.11.4 "Supported handler method arguments and return types"疯狂的概述。另请注意,您可以 Autowiring validator

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

}

关于java - Spring MVC 中的注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/803495/

相关文章:

spring - Spring 事务管理器是否将连接绑定(bind)到线程?

spring-mvc - Spring MVC : How to use a request-scoped bean inside a spawned thread?

java - 内容类型为 application/x-www-form-urlencoded 的 Http Post 请求在 Spring 中不起作用

java - takeWhile 是模拟循环中断的正确方法吗

java - 如 Effective Java 一书中所述,理解 Java 中非静态成员类的创建

java - 为复杂对象生成 JSON 消息

java - 在较小的应用程序中替代 Spring

java - 通过环境属性实现Spring服务

Java——继承和多态

java - 如何在java中反向乘以有符号除法字节数组?