0

here is the code:- form.html :

<div class="container">
    <div class="row mt-5">
        <div class="col-md-6 offset-md-3">
    
        
            <form th:action="@{/process}" method="post" th:object="${loginData}">
            
                <div class="mb-3">
                    <label for="exampleInputEmail1" class="form-label">Email address</label> 
                    <input 
                        type="text"
                        name="userName" 
                        class="form-control"
                        
                        th:value="${loginData.userName}"
                        
                        id="exampleInputEmail1" 
                        aria-describedby="emailHelp">
                        
                </div>
                
                
                <div class="mb-3">
                    <label for="exampleInputPassword1" class="form-label">Password</label>
                    
                    <input 
                        type="email"
                        name="email" 
                        
                        th:value="${loginData}"
                        
                        class="form-control"
                        id="exampleInputPassword1">
                </div>
                
                
                <div class="mb-3 form-check">
                    <input 
                        type="checkbox" 
                        class="form-check-input" 
                        id="exampleCheck1">
                    
                    <label class="form-check-label" for="exampleCheck1">Check me out</label>
                </div>
                
                <button type="submit" class="btn btn-primary">Submit</button>
                
            </form>
            
        </div>
    </div>
</div>

MyController :

@GetMapping("/form") public String formHandler(Model m ) {

            System.out.println("opening form");
            LoginData loginData = new LoginData();
            if(loginData != null ) {
                m.addAttribute("login", new LoginData());
                System.out.println("YAY");
            }else {
                System.out.println("Bhag Bh*****ke");
            }
        
            return "form1";
        }
        
 //handler for process form
 @PostMapping("/process")
 public String processform(@ModelAttribute("loginData") LoginData loginData) {
            
    System.out.println(loginData);
    return"success";
}

success.html :

  <!doctype html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
      <meta charset="UTF-8" />
      <title>Success</title>
    </head>
    <body>
          <h1>Welcome to this success page</h1>
          <h1 th:text="${LoginData.userName}"></h1>
          <h1 th:text="${LoginData.email}"></h1>
          
    </body>
    </html>

saying error : Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'userName' cannot be found on null

Login page is not ruuning because LoginData is empty at begining

2 Answers2

0

You haven't exactly specified which of the 2 pages are causing the issue, but I'm going to guess that it is the first one.

I would first try replacing the th:value tag with th:field in the username input:

<input 
type="text"
name="userName" 
class="form-control"
th:field="*{userName}"
id="exampleInputEmail1" 
aria-describedby="emailHelp">

Since you have already defined the loginData object within the form tag:

th:object="${loginData}

The next input tag could also cause some issues, I'm guessing that this should accept the user's password:

<input
type="email" 
name="email" 
th:value="${loginData}" 
class="form-control" 
id="exampleInputPassword1">

You would want to update it to something like:

<input
    type="password" 
    th:field="*{password}" 
    class="form-control" 
    id="exampleInputPassword1">
0

The exception tells you that there is no model attribute with the name loginData. looking at the controller code after this that can be confirmed as you are adding a model attribute with name login not loginData m.addAttribute("login", new LoginData());

Also I'm not usre why you define a loginData variable in your controller, check if it's non null and than don't use it but create a new one

LoginData loginData = new LoginData();
if(loginData != null ) {
    m.addAttribute("login", new LoginData());
    System.out.println("YAY");
}
Ralan
  • 651
  • 6
  • 17