【问题标题】:One field depends on another in thymeleaf with spring boot一块田地依赖于百里香中的另一块田地,带有弹簧靴
【发布时间】:2023-01-17 17:53:31
【问题描述】:

我有一个使用 thymeleaf 的 spring boot 应用程序。在前端页面上,我有一个名为 calulatedValue 的输入字段,后面跟着另一个字段名称 OverridingValue。验证应该是 OverridingValue 总是小于 calculatedValue。例如,calculatedValue 为 2,OverridingValue 应小于 2。如果用户输入的值 >=2,则在表单上应为他显示一条错误消息。如何使用 thymeleaf 和 spring boot 实现此验证。

【问题讨论】:

    标签: java spring spring-boot thymeleaf


    【解决方案1】:

    在你的 template.html 中:

            <div th:if="${error}">
                <div>
                    <span th:utext="${error}"></span>
                </div>
            </div>
    

    在你的 Controller.java 中:

    @GetMapping("/{calculatedValue}/{overridingValue}")
    public String checkValues(final RedirectAttributes redirectAttributes, @RequestParam("calculatedValue") Integer calculatedValue, @RequestParam("overridingValue") Integer overridingValue) {
        if (calculatedValue >= overridingValue) {
            redirectAttributes.addFlashAttribute("error", "Invalid number!");
            return "redirect:/";
        }
        return "template";
    }
    

    【讨论】:

      猜你喜欢
      • 2018-04-02
      • 2021-12-11
      • 1970-01-01
      • 2018-10-12
      • 2014-06-25
      • 1970-01-01
      • 2018-11-24
      • 1970-01-01
      相关资源
      最近更新 更多