0

Thymeleaf method fields.errors() which is to collect all validation, is not showing errors in html pages, and gives error i have tried all the methods to capture the error. also tried the article

Main Error : "Neither BindingResult nor plain target object for bean name 'user' available as request attribute"

Error >>

Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:153)
    at org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903)
    at org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:227)
    at org.thymeleaf.spring5.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:306)
    at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:253)
    at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:227)
    at org.thymeleaf.spring5.processor.AbstractSpringFieldTagProcessor.doProcess(AbstractSpringFieldTagProcessor.java:174)
    at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74)
    ... 69 more
    
    Caused by: org.attoparser.ParseException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "user_login" - line 28, col 21)
    at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393)
    at org.attoparser.MarkupParser.parse(MarkupParser.java:257)
    at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230)
    ... 48 more
    Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "user_login" - line 28, col 21)

Controller class

@GetMapping("/user_login")
public String getUserLoginPage(Model model){
    LoginModel user_model  = new LoginModel();
    model.addAttribute("title", "User Login Page");
    model.addAttribute("user", user_model);
    return "user_login";
}

// user login Handler
@PostMapping("/process_user_login")
public String processUserLogin(
    @Valid @ModelAttribute LoginModel user,
    BindingResult bindingResult,Model model){
    
    if(bindingResult.hasErrors()){
        logger.info("user login from has some error");

        return "user_login";
    }
    model.addAttribute("user", user);
    return "user_login";
}

Model class

package com.attendance.model;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

public class LoginModel {

    @NotBlank(message = "email cannot be blank")
    @Size(min = 3, max = 20, message = " 3 -20 charater" )
    private String userEmail;

    @NotBlank(message = "password cannot be blank")
    private String password;

    public LoginModel() {
        super();
    }
    
    public LoginModel(String userEmail, String password) {
        super();
        this.userEmail = userEmail;
        this.password = password;
    }
    public String getUserEmail() {
        return userEmail;
    }
    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }
    public String getPassword() {
        return password;    
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "LoginModel [userEmail=" + userEmail + ", password=" + password + "]";
    }

}

HTML Page/View

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:replace="base::layout(~{::section})">
   <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <style rel="stylesheet" th:href="@{/css/userlogin.css}" ></style>
      <title>Home Page </title>
   </head>
   <body>
      <section>
         <div class="card card-container mb-4 pb-5">
            <!-- <img class="profile-img-card" src="//lh3.googleusercontent.com/-6V8xOA6M7BA/AAAAAAAAAAI/AAAAAAAAAAA/rzlHcD0KYwo/photo.jpg?sz=120" alt="" /> -->
            <img id="profile-img" class="profile-img-card" src="//ssl.gstatic.com/accounts/ui/avatar_2x.png" />
            <p id="profile-name" class="profile-name-card"></p>
            <form th:object="${user}" th:action="@{/process_user_login}" method="post" class="form-signin">
               <span id="reauth-email" class="reauth-email"></span>
               <input 
                  type="email"
                  name="userEmail"
                  th:field="*{userEmail}"
                  id="inputEmail" 
                  class="form-control" 
                  placeholder="Email address"
                  autofocus />
               <p th:if="${#fields.hasErrors('userEmail')}" >Incorrect date</p>
               <!--  <p th:each="e : ${#fields.errors('userEmail')}" th:text="${e}" ></p> -->
               <input 
                  type="password" 
                  name="password"
                  th:field="*{password}"
                  id="inputPassword" 
                  class="form-control" 
                  placeholder="Password" />
               <div id="remember" class="checkbox">
                  <label>
                  <input type="checkbox" value="remember-me"> Remember me
                  </label>
               </div>
               <button class="btn btn-lg btn-primary btn-block btn-signin" type="submit">Sign in</button>
            </form>
            <!-- /form -->
            <a href="#" class="forgot-password">
            Forgot the password?
            </a>
         </div>
         <!-- /card-container -->
      </section>
   </body>
</html>
Kunal Varpe
  • 419
  • 1
  • 5
  • 28
Jitendra
  • 31
  • 1
  • 4

1 Answers1

0

In Thymeleaf, you can use the th:field attribute to bind a form field to a field in a model object, and the th:errors attribute to display any validation errors for that field.

For example, if you have a model object called user with a field called email, and you want to display an input field for the email address and any validation errors, you could use the following code:

<form>
  <label for="email">Email:</label>
  <input type="text" th:field="*{user.email}" id="email" />
  <div th:if="${#fields.hasErrors('user.email')}" th:errors="*{user.email}">Email error</div>
</form>

If the email field is not showing any validation errors, it is possible that the field is not being validated or that the validation is not being triggered. To troubleshoot this issue, you can try the following:

Make sure that the model object is being passed to the Thymeleaf template correctly, and that the field is being bound to the correct model object field. Make sure that the field is being validated on the server side, and that the validation rules are being applied correctly. Make sure that the form is being submitted correctly, and that the validation is being triggered when the form is submitted. If you are still having trouble displaying the validation errors for the model object field, it may be helpful to review the documentation for the th:field and th:errors attributes, as well as the Thymeleaf Validation Dialect, to ensure that you are using them correctly. You may also want to consider debugging the validation process on the server side to identify any issues.