title: Error Handling in Thymeleaf and Spring boot
body: <p>In my spring boot thymeleaf application, i am struggling to figure out how to display the error message returned from the service layer onto the UI.</p>
  <p>My UI Code (index.html) is</p>
  <pre class="lang-html prettyprint-override"><code>&lt;div class=&quot;u-expanded-width-xs u-form u-form-1&quot;&gt;
      &lt;form class=&quot;u-clearfix u-form-horizontal u-form-spacing-10 u-inner-form&quot;
            method=&quot;POST&quot;
            modelAttribute=&quot;indexFormBean&quot; name=&quot;form&quot;
            style=&quot;padding: 0;&quot;
            th:action=&quot;@{/home}&quot; th:object=&quot;${indexFormBean}&quot;&gt;
          &lt;div class=&quot;u-form-group u-form-name u-form-group-1&quot;&gt;
              &lt;label for=&quot;email-dbf3&quot;
                     class=&quot;u-form-control-hidden u-label&quot;&gt;Email&lt;/label&gt;
              &lt;input type=&quot;email&quot; placeholder=&quot;Enter a valid email address&quot;
                     id=&quot;email-dbf3&quot; name=&quot;email&quot;
                     class=&quot;u-border-1 u-border-grey-30 u-input u-input-rectangle u-white&quot;
                     required=&quot;true&quot; autofocus=&quot;autofocus&quot;&gt;
          &lt;/div&gt;
          &lt;div class=&quot;u-form-email u-form-group u-form-group-2&quot;&gt;
              &lt;label for=&quot;name-dbf3&quot;
                     class=&quot;u-form-control-hidden u-label&quot;&gt;Name&lt;/label&gt;
              &lt;input type=&quot;text&quot; placeholder=&quot;Enter a valid password&quot; id=&quot;name-dbf3&quot;
                     name=&quot;password&quot;
                     class=&quot;u-border-1 u-border-grey-30 u-input u-input-rectangle u-white&quot;
                     required=&quot;true&quot;&gt;
          &lt;/div&gt;
          &lt;div class=&quot;u-align-left u-form-group u-form-submit u-form-group-3&quot;&gt;
              &lt;button type=&quot;submit&quot; name=&quot;submit&quot; class=&quot;btn btn-primary btn-lg&quot;&gt;Login
              &lt;/button&gt;
          &lt;/div&gt;
          &lt;p th:if=&quot;${#fields.hasErrors('email')}&quot;  th:errors=&quot;*{email}&quot;&gt;Description errors&lt;/p&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  </code></pre>
  <p>The controller method invoked when the form is submitted is like this</p>
  <pre class="lang-java prettyprint-override"><code>@PostMapping(&quot;/home&quot;)
  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 &quot;index&quot;;
      }
      return &quot;home&quot;;
  }
  </code></pre>
  <p>Now if the <code>authenticate</code> method needs to show some error on the UI (index.html) how can I do so  ?</p>