java - Spring中的@Valid注解表示什么?

标签 java spring spring-mvc spring-annotations

在以下示例中,ScriptFile 参数使用 @Valid 注释进行标记。

@Valid注解有什么作用?

@RequestMapping(value = "/scriptfile", method = RequestMethod.POST)    
public String create(@Valid ScriptFile scriptFile, BindingResult result, ModelMap modelMap) {    
    if (scriptFile == null) throw new IllegalArgumentException("A scriptFile is required");        
    if (result.hasErrors()) {        
        modelMap.addAttribute("scriptFile", scriptFile);            
        modelMap.addAttribute("showcases", ShowCase.findAllShowCases());            
        return "scriptfile/create";            
    }        
    scriptFile.persist();        
    return "redirect:/scriptfile/" + scriptFile.getId();        
}    

最佳答案

用于验证目的。

Validation It is common to validate a model after binding user input to it. Spring 3 provides support for declarative validation with JSR-303. This support is enabled automatically if a JSR-303 provider, such as Hibernate Validator, is present on your classpath. When enabled, you can trigger validation simply by annotating a Controller method parameter with the @Valid annotation: After binding incoming POST parameters, the AppointmentForm will be validated; in this case, to verify the date field value is not null and occurs in the future.


更多信息请看这里:
http://blog.springsource.com/2009/11/17/spring-3-type-conversion-and-validation/

关于java - Spring中的@Valid注解表示什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3595160/

相关文章:

java - Websphere 上传 EAR 文件,堆问题

java - 如何使用 Spring 在抽象类中注入(inject)静态方法的局部变量?

java - Spring MVC 显示日期变量

java - 如何让Javamelody使用不同的端口(Spring Boot+暴露的两个HTTP端口)

java - 如何将spring MVC应用程序注册到spring boot admin

java - Spring : boolean variables in objects get returned

java - 如何在 O(n) 时间内找到到达数组末尾的最少跳转次数

java - “The libsDir method has been deprecated.”不使用时如何修复?

java - 基本的java打印

spring - 如何通过 spring boot 和 ejb 集成修复 hibernate validator 问题