@Valid 注释在 Spring 中表示什么? [英] What does the @Valid annotation indicate in 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 提供程序,例如休眠验证器,存在于你的类路径.启用后,您可以简单地触发验证注释控制器方法带有@Valid 注释的参数:绑定传入 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屋!