【问题标题】:Spring - BindingResultSpring - BindingResult
【发布时间】:2020-09-05 19:24:56
【问题描述】:

我不知道为什么,当我发布空表单时出现错误

org.springframework.expression.spel.SpelEvaluationException: EL1007E: 在 null 上找不到属性或字段“地址”

在我发布表单之前:

    @GetMapping("/new")
public String showCreateFormForEmployeeAndAddresses(Model model) {

    if(addressService.getCheck()== false) {
        for (int i = 1; i <= 2; i++) {
            addressService.newAddress(new Address());
        }
    }

    model.addAttribute("employee", new Employee()).addAttribute("form", addressService);
    return "new_employee_form";
}

在我发布空表单后:(在填写的表单中一切正常)

   @RequestMapping(value = "/employees", method = RequestMethod.POST)
public String saveEmployeeAndAddress(@ModelAttribute @Valid Employee employee,
                                     BindingResult bindingResultEmployee,
                                     @ModelAttribute @Valid AddressRepository addresses,
                                     BindingResult bindingResultAddressRepository, Model model)  {

    if(bindingResultEmployee.hasErrors() || bindingResultAddressRepository.hasErrors()) {

        return "new_employee_form";

    } else{
        ExecutorService executor = Executors.newSingleThreadExecutor();
        Runnable runnableEmployee = () -> employeeService.saveEmployeeToDB(employee);
        List<Address> addressesFromForm = addresses.getAddresses();
        Runnable runnableAddress = () -> addressService.saveAddressToDB(addressesFromForm);

        executor.submit(runnableEmployee);
        executor.submit(runnableAddress);

        return "redirect:/employees";
    }
}

在这行之后显示:return "new_employee_form";

在控制台中我也有:

[THYMELEAF][http-nio-8080-exec-2] Exception processing template "new_employee_form": Exception evaluating SpringEL expression: "addresses" (template: "new_employee_form" - line 57, col 30)
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'addresses' cannot be found on null

和视图:

   <div th:each="address, stat : *{addresses}">
                        <span th:switch="*{addresses[__${stat.index}__].type}">
                            <div style = "text-align: center;" th:case="P" ><h5>Adres stały</h5></div>
                            <div style = "text-align: center;" th:case="C" ><h5>Adres korespondencyjny</h5></div>
                        </span>
                        <div class="form-group">
                            <input type="hidden" class="form-control" th:field="*{addresses[__${stat.index}__].type}"/>
                        </div>
                        <div class="form-group">
                            <input type="text" class="form-control" th:field="*{addresses[__${stat.index}__].street}"/>
                            <label class="control-label">Ulica</label>
                            <div class="text-danger"><p th:if="${#fields.hasErrors('addresses[__${stat.index}__].street')}" th:errors="*{addresses[__${stat.index}__].street}"/></div>
                        </div>
                        <div class="form-group">
                            <input type="text" class="form-control" th:field="*{addresses[__${stat.index}__].streetNr}"/>
                            <label class="control-label">Numer domu</label>
                            <div class="text-danger"><p th:if="${#fields.hasErrors('addresses[__${stat.index}__].streetNr')}" th:errors="*{addresses[__${stat.index}__].streetNr}"/></div>
                        </div>
                        <div class="form-group">
                            <input type="number" class="form-control" th:field="*{addresses[__${stat.index}__].flatNr}"/>
                            <label class="control-label">Numer mieszkania</label>
                            <div class="text-danger"><p th:if="${#fields.hasErrors('addresses[__${stat.index}__].flatNr')}" th:errors="*{addresses[__${stat.index}__].flatNr}"/></div>
                        </div>
                        <div class="form-group">
                            <input type="text" class="form-control" th:field="*{addresses[__${stat.index}__].postalCode}"/>
                            <label class="control-label">Kod pocztowy</label>
                            <div class="text-danger"><p th:if="${#fields.hasErrors('addresses[__${stat.index}__].postalCode')}" th:errors="*{addresses[__${stat.index}__].postalCode}"/></div>
                        </div>
                        <div class="form-group">
                            <input type="text" class="form-control" th:field="*{addresses[__${stat.index}__].city}"/>
                            <label class="control-label">Miasto</label>
                            <div class="text-danger"><p th:if="${#fields.hasErrors('addresses[__${stat.index}__].city')}" th:errors="*{addresses[__${stat.index}__].city}"/></div>
                        </div>
                        <div class="form-group">
                            <input type="text" class="form-control" th:field="*{addresses[__${stat.index}__].country}"/>
                            <label class="control-label">Kraj</label>
                            <div class="text-danger"><p th:if="${#fields.hasErrors('addresses[__${stat.index}__].country')}" th:errors="*{addresses[__${stat.index}__].country}"/></div>
                        </div>
                    </div>

【问题讨论】:

  • 这可能有助于this

标签: spring


【解决方案1】:

返回的视图new_employee_form 需要为其提供addresses 属性。

看起来@ModelAttribute @Valid AddressRepository addresses 参数为空,所以必须手动提供:

if(bindingResultEmployee.hasErrors() || bindingResultAddressRepository.hasErrors()) {
    model.addAtribute("addresses", an_instance_of_whatever_addresses_is)
    return "new_employee_form";
}

查看给定的代码,我希望 Getter 方法 (showCreateFormForEmployeeAndAddresses(Model model)) 将它提供给模型(否则它的验证将始终为空失败)。

无论如何,我强烈建议您重新评估命名模型属性类和变量的方式,因为像 AddressRepositoryaddressService 这样的名称在 Spring MVC 中具有完全不同的含义。

【讨论】:

  • 感谢您的帮助。我手动添加模型属性。现在没有错误。员工表格已经过验证,但不幸的是地址没有。 “an_instance_of_whatever_addresses_is”地址是一个 LIST
    列表和两个地址。所以目前在返回之前我有 model.addAttribute("employee", employee).addAttribute("form",addresses);并查看zapodaj.net/b7841cb1fdb73.png.html
猜你喜欢
  • 2011-05-06
  • 1970-01-01
  • 2018-05-19
  • 2017-06-27
  • 2011-10-05
  • 1970-01-01
  • 1970-01-01
  • 2012-05-11
  • 1970-01-01
相关资源
最近更新 更多