Search code examples
javahtmlspringthymeleaf

Thymeleaf not recognizing object


I tried following the example that I had of a working login page for submitting a new product but when I try to launch the admin dashboard it returns status code 500 and in the launch console an error stating: "org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/AdminDashboard.html]") ... Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'newProd' available as request attribute"

Heres my code:

HTML file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet"
          href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
          integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
          crossorigin="anonymous">
</head>
<body>
  <h1>Admin dashboard</h1>


  <form th:action="@{/adminDashboard}" method="post" th:object="${newProd}">
      <div class="form-group">
          <label class="control-label" for="name"> Product name </label>
          <input id="name" class="form-control" th:field="*{name}" required autofocus="autofocus" />
      </div>
      <div class="form-group">
          <label class="control-label" for="storedAmount"> stored Amount </label>
          <input id="storedAmount" class="form-control" th:field="*{storedAmount}" required autofocus="autofocus" />
      </div>
      <div class="form-group">
          <label class="control-label" for="price"> Price </label>
          <input id="price" class="form-control" th:field="*{price}" required autofocus="autofocus" />
      </div>
      <div class="form-group">
          <div class="row">
              <div class="col-sm-6 col-sm-offset-3">
                  <input type="submit" name="Product-submit" id="Product-submit"
                         class="form-control btn btn-primary" value="Submit product" />
              </div>
          </div>
      </div>
  </form>

</body>
</html>

Controller:

public class MainController {
    @GetMapping("/adminDashboard")
    public String showAdminDashboard(){
        return "AdminDashboard";

    }

    @PostMapping("/adminDashboard")
    public String loginUserAccount(@ModelAttribute("newProd") ProductDto productDto) {
        System.out.println(productDto.getName());
        return "redirect:/dashboard";
    }
}

ProductDto:

package com.reiniskr.registrationloginspring.web.dto;

public class ProductDto {
    private String name;
    private int storedAmount;
    private double price;

    public ProductDto(){}

    public ProductDto(String name, int storedAmount, double price) {
        this.name = name;
        this.storedAmount = storedAmount;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getStoredAmount() {
        return storedAmount;
    }

    public void setStoredAmount(int storedAmount) {
        this.storedAmount = storedAmount;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}

If i remove the POST form in the HTML file it works, but anything other that I do with thymeleaf it just doesnt load the template at all.


Solution

  • You need to add an instance of ProductDto to the Model as the newProd attribute before showing the page.

    @GetMapping("/adminDashboard")
    public String showAdminDashboard(Model model) {
        model.addAttribute("newProd", new ProductDto());
        return "AdminDashboard";
    }