我目前在项目中使用以下代码,我遇到的问题是即使bindingresult中存在错误(bindingResult.hasErrors()为true),它也会在百万美元结果中呈现为false。这让我觉得bindingResult没有正确“注入”。我在以下代码中做错了什么?
<form action="blog.html" th:action="@{/fileUpload}" method="post"
enctype="multipart/form-data" th:object="${form}">
<input type="text" name="title" th:field="*{title}" /> <input
type="text" name="content" th:field="*{content}" /> <input
type="file" name="myFile" th:field="*{myFile}" /> <input
type="submit" />
<div id="errors" class="alert alert-error">
<ul th:if="${#fields.hasErrors('*')}">
<li th:each="err : ${#fields.errors('*')}" th:text="${err}"></li>
</ul>
<div th:if="${#fields.hasGlobalErrors()}">
<p th:each="err : ${#fields.globalErrors()}" th:text="${err}">...</p>
</div>
</div>
</form>
控制器
@RequestMapping(value = "/blog", method = RequestMethod.GET)
public String getIndex(Model model) {
model.addAttribute("form", new AddBlogForm());
return "blog";
}
@RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
public String importParse(Model model, @Valid AddBlogForm form, BindingResult bindingResult) {
model.addAttribute("form", form);
try {
if (!bindingResult.hasErrors()) {
model.addAttribute("successmessage", "Succesfully added");
blogSrv.addPost(form.getTitle(), form.getContent(), form.getMyFile());
model.addAttribute("form", new AddBlogForm());
}
return "blog";
} catch (IllegalStateException e) {
bindingResult.addError(new ObjectError("image", "IllegalStateException occured " + e.getMessage()));
return "blog";
} catch (IOException e) {
bindingResult.addError(new ObjectError("image", "IOException occured " + e.getMessage()));
return "blog";
}
}
答案 0 :(得分:1)
您正在尝试使用Springs数据绑定,使用框架。
首先从方法签名中删除Model
属性,然后在成功保存后使用重定向,最后将@ModelAttribute
添加到AddBlogForm
注释中。
@RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
public String importParse(@Valid @ModelAttribute AddBlogForm form, BindingResult bindingResult) {
try {
if (!bindingResult.hasErrors()) {
blogSrv.addPost(form.getTitle(), form.getContent(), form.getMyFile());
return "redirect:/blog";
}
} catch (IllegalStateException e) {
bindingResult.addError(new ObjectError("image", "IllegalStateException occured " + e.getMessage()));
} catch (IOException e) {
bindingResult.addError(new ObjectError("image", "IOException occured " + e.getMessage()));
}
return "blog"
}
如果您确实要向模型添加成功消息,请改用RedirectAttributes
并使用Flash消息,以便在重定向后可用。
@RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
public String importParse(@Valid @ModelAttribute AddBlogForm form, BindingResult bindingResult, RedirectAttributes attrs) {
try {
if (!bindingResult.hasErrors()) {
atts.setFlashAttribute("successmessage", "Succesfully added");
blogSrv.addPost(form.getTitle(), form.getContent(), form.getMyFile());
return "redirect:/blog";
}
} catch (IllegalStateException e) {
bindingResult.addError(new ObjectError("image", "IllegalStateException occured " + e.getMessage()));
} catch (IOException e) {
bindingResult.addError(new ObjectError("image", "IOException occured " + e.getMessage()));
}
return "blog"
}