【问题标题】:Spring Boot - not getting a validation errorsSpring Boot - 没有收到验证错误
【发布时间】:2018-03-22 13:59:18
【问题描述】:

我正在尝试验证表单。 Spring 正确验证,但是当应该返回错误时,它会收到一个异常:

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'employee' available as request attribute

添加我不知道出了什么问题。你能帮帮我吗?

控制器:

 @RequestMapping(value = "/employee/add",method = RequestMethod.POST)
    public String addEmployee(@Valid DTOEmployee dtoEmployee, BindingResult result) {

        if (result.hasErrors()) {
            return "employee_add";
        }
        employeeService.save(dtoEmployee);

        return "redirect:/employee?add";
    }

DTO员工:

public class DTOEmployee{

        @NotNull
        private String name;
        @NotNull
        private String subname;
        @NotNull
        private String email;

    }

employee_add 的片段:

 <form th:action="@{/employee/add}" th:object="${employee}" method="post" class="form-inline justify-content-center">
                                    <div class="input-group my-1">
                                        <input th:field="*{name}" type="text" id="name" class="form-control"
                                                    placeholder="Your name"/>
                                        <p th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name error</p>
            <input th:field="*{subname}" type="text" id="name" class="form-control"
                                                    placeholder="Your subname"/>
                                        <p th:if="${#fields.hasErrors('subname')}" th:errors="*{name}">Name error</p>
                                        <input th:field="*{email}" type="email" id="email" class="form-control"
                                                    placeholder="Your mail"/>
                                        <p th:if="${#fields.hasErrors('email')}" th:errors="*{email}">Name error</p>

                                    </div>
                                        <button type="submit" class="btn btn-primary">Save</button>
                                </form>

messages.properties:

NotNull.DTOEmployee.name = Name must be not null
NotNull.DTOEmployee.subname= Subname must be not null
NotNull.DTOEmployee.email= Email must be not null

【问题讨论】:

    标签: spring validation thymeleaf


    【解决方案1】:

    似乎您将 thymeleaf 表单提交与请求混合在一起。如来自 spring.io https://spring.io/guides/gs/handling-form-submission/

    的示例所述
    @PostMapping("/greeting")
    public String greetingSubmit(@ModelAttribute Greeting greeting) {
        return "result";
    }
    

    你好像不见了@ModelAttribute

    【讨论】:

      【解决方案2】:

      嗯...我添加了@ModelAttribute [ public String addEmployee(@Valid @ModelAttribute DTOEmployee dtoEmployee, BindingResult result)],我得到以下异常:

      Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "employee_add" - line 44, col 44)] with root cause
      

      employee_add 中的第 44 行是:

      <input th:field="*{name}" type="text" id="name" class="form-control"
      

      编辑:

      我忘记在引号中添加属性名称:

      public String addEmployee(@Valid @ModelAttribute("employee") DTOEmployee dtoEmployee, BindingResult result)
      

      它的工作:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-04
        • 2022-02-03
        • 2017-12-28
        • 2020-08-19
        • 2021-09-14
        • 1970-01-01
        • 1970-01-01
        • 2020-06-18
        相关资源
        最近更新 更多