在以下示例中,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();
}

最佳答案

这是出于验证目的。



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

10-07 16:19