字段错误和全局错误在 Thymeleaf 中保持为空

Fields error and globalerrors stay empty in Thymeleaf

我目前在项目中使用以下代码,我遇到的问题是即使绑定结果中存在错误(bindingResult.hasErrors() 为真),它在 thymeleaf 结果中呈现为假。这让我觉得 bindingResult 不是 "injected" 正确。我在下面的代码中做错了什么吗?

<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";
        }
}

您正在尝试绕过 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"
}