在SpringBoot中验证表单输入-thymeleaf

问题描述 投票:0回答:1

我有这个控制器:

@Controller
@Slf4j
@RequestMapping("/natalchart")
public class NatalChartDataController {

    @GetMapping({"/data"})
    public String data(Model model, NatalChartDataPayload data) {
        model.addAttribute("months", Month.values());
        return "natalChartData";

    }

    @ModelAttribute("data")
    public NatalChartDataPayload natalChartDataPayload(){
        return NatalChartDataPayload.builder().build();
    }

    @PostMapping("/create")
    public String createNatalChart(@Valid @ModelAttribute NatalChartDataPayload data,
                                   BindingResult result) {

        if (result.hasErrors()) {
            return "natalChartData";
        }

..
}

这是模板:

 <div class="container" data-aos="fade-up">
          <form method="post" th:action="@{/natalchart/create}" th:object="${data}">
          <div class="row">
            <div class="col-lg-8">
                <h4>Enter your birth details to create your natal chart</h4>

              <p>&nbsp;</p>
              <p>&nbsp;</p>

              
              <div th:if="${#fields.hasErrors('email')}" th:errors="*{email}">Email Error</div>

和错误:

enter image description here

但我在模板中没有看到错误消息

java spring-boot spring-mvc thymeleaf spring-thymeleaf
1个回答
0
投票

该错误看起来像模型属性仍然命名为默认的“natalChartDataPayload”而不是预期的“数据”。

© www.soinside.com 2019 - 2024. All rights reserved.