验证表单时,我的行为很奇怪。
一旦添加了Hibernate @Valid批注,如果发布的数据无效,Tomcat会将我的请求视为“错误”。如果数据有效,则无后顾之忧。
我用:
目前,我做了一个非常简单的验证:
public class RemoveCacheElementForm { @NotBlank(message = "Please select a cache name.") private String name; @NotBlank(message = "Please select a cache entry key.") private String key;
Spring控制器:
/** * Handler to remove one cached elements from the specified cache. * * @return the view. */ @RequestMapping(value = CACHE_REMOVE, method = RequestMethod.POST) public String removeCachedElement(ModelMap model, @Valid @ModelAttribute(FORM_NAME) RemoveCacheElementForm form) { model.addAttribute("removeElementResult", CacheUtils.removeCachedElement(form.getName(), form.getKey())); initializeModel(model); return CACHE_ADMIN_PAGE; }
当我删除@Valid注释时,也不必担心。
有人有主意吗?
非常感谢你的帮助!:-)
尝试将代码更改为
@RequestMapping(value = CACHE_REMOVE, method = RequestMethod.POST) public String removeCachedElement(ModelMap model, @Valid @ModelAttribute(FORM_NAME) RemoveCacheElementForm form, BindingResult bindingResult) { model.addAttribute("removeElementResult", CacheUtils.removeCachedElement(form.getName(), form.getKey())); initializeModel(model); return CACHE_ADMIN_PAGE; }