Thymeleaf和Spring引导中的错误处理

问题描述

在我的Spring Boot thymeleaf应用程序中,我正在努力弄清楚如何将从服务层返回的错误消息显示在UI上。

我的UI代码(index.html)是

<div class="u-expanded-width-xs u-form u-form-1">
    <form class="u-clearfix u-form-horizontal u-form-spacing-10 u-inner-form"
          method="POST"
          modelAttribute="indexFormBean" name="form"
          style="padding: 0;"
          th:action="@{/home}" th:object="${indexFormBean}">
        <div class="u-form-group u-form-name u-form-group-1">
            <label for="email-dbf3"
                   class="u-form-control-hidden u-label">Email</label>
            <input type="email" placeholder="Enter a valid email address"
                   id="email-dbf3" name="email"
                   class="u-border-1 u-border-grey-30 u-input u-input-rectangle u-white"
                   required="true" autofocus="autofocus">
        </div>
        <div class="u-form-email u-form-group u-form-group-2">
            <label for="name-dbf3"
                   class="u-form-control-hidden u-label">Name</label>
            <input type="text" placeholder="Enter a valid password" id="name-dbf3"
                   name="password"
                   class="u-border-1 u-border-grey-30 u-input u-input-rectangle u-white"
                   required="true">
        </div>
        <div class="u-align-left u-form-group u-form-submit u-form-group-3">
            <button type="submit" name="submit" class="btn btn-primary btn-lg">Login
            </button>
        </div>
        <p th:if="${#fields.hasErrors('email')}"  th:errors="*{email}">Description errors</p>
    </form>
</div>

提交表单时调用的控制器方法如下

@PostMapping("/home")
public String authenticate(@ModelAttribute IndexFormBean indexFormBean,Model model){
    String loginResponse = userService.login(indexFormBean.getEmail(),indexFormBean.getPassword());
    if(StringUtils.isEmpty(loginResponse)){
        //Some error that is returned from the service layer
        return "index";
    }
    return "home";
}

现在,如果authenticate方法需要在UI(index.html)上显示一些错误,该怎么办?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)