gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:01:43 28 4
gpt4 key购买 nike

在以下示例中,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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com