@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/