@Valid注释在Spring中表示什么? [英] What does the @Valid annotation indicate in Spring?

查看:97
本文介绍了@Valid注释在Spring中表示什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下示例中, ScriptFile 参数标有 @Valid 注释。

In the following example, the ScriptFile parameter is marked with an @Valid annotation.

@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();        
}    


推荐答案

用于验证目的。


验证通常在绑定用户输入后验证
模型。
Spring 3使用JSR-303为
声明性验证提供支持。
如果JSR-303提供程序(例如
Hibernate Validator)出现在
类路径中,则自动启用此支持
。启用后,您可以通过
触发验证,只需使用@Valid注释注释一个Controller方法
参数:
绑定传入的POST
参数后,AppointmentForm将为
经过验证;在这种情况下,要验证
,日期字段值不为空,将来会出现

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/

这篇关于@Valid注释在Spring中表示什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆