Neither BindingResult nor plain target object for bean name 'product' available as request attribute

Issue

I can’t seem to find the error in my code. All my imports are fine. Everything seems in order but when I click the add new button, I get the error, "Neither BindingResult nor plain target object for bean name ‘product’ available as request attribute". Also, if I change the bean name to products in my Get and Post Mapping request along with making the same change in the add file, it works.

Here’s my add.html file:

<!doctype html>
<nav th:replace="/fragments/nav :: nav-admin"></nav>

<div class="container">

    <h1 class="display-2">Add a product</h1>
    <a href="/admin/products" class="btn btn-primary mt-4 mb-4">Back to Products</a>
    
    <div th:if="${message}" th:text="${message}" th:class="${'alert ' + alertClass}"></div>

    <form method="post" th:object="${product}" th:action="@{/admin/products/add}" enctype="multipart/form-data">

        <div th:if="${#fields.hasErrors('*')}" class="alert alert-danger">
            There are errors
        </div>
    
        <div class="form-group">
            <label for="">Name</label>
            <input type="text" class="form-control" th:field="*{name}" placeholder="Name">
            <span class="error" th:if="${#fields.hasErrors('name')}" th:errors="*{name}"></span>
        </div>
        
        <div class="form-group">
            <label for="">Description</label>
            <textarea th:field="*{description}" rows="10" class="form-control" placeholder="Description"></textarea>
            <span class="error" th:if="${#fields.hasErrors('description')}" th:errors="*{description}"></span>
        </div>

        <div class="form-group">
            <label for="">Image</label>
            <input type="file" class="form-control" th:name="file" th:id="file">
            <img class="mt-2" src="#" alt="" id="imgPreview1">
        </div>

        <div class="form-group">
            <label for="">Price</label>
            <input type="text" class="form-control" th:field="*{price}" placeholder="20 or 20.99">
            <span class="error" th:if="${#fields.hasErrors('price')}" th:errors="*{price}"></span>
        </div>

        <div class="form-group">
            <label for="">Category</label>
            <select th:field="*{categoryId}" class="form-control">
                <option th:value="0">Choose a category</option>
                <option th:each="cat: ${categories}" th:value="${cat.id}" th:text="${cat.name}"></option>
            </select>
            <span class="error" th:if="${#fields.hasErrors('categoryId')}" th:errors="*{categoryId}"></span>
        </div>

        <button class="btn btn-danger mt-4 mb-4">Add</button>

    </form>

</div>

<div th:replace="/fragments/footer"></div>

My model name is Products.java and here is the Controller code for GET and POST for add

@GetMapping("/add")
public String add(Products product, Model model) {
    
    List<Category> categories = categoryRepo.findAll();
    model.addAttribute("categories", categories);

    return "admin/products/add";
}

@PostMapping("/add")
public String add(@Valid Products product, BindingResult bindingResult, 
                    MultipartFile file, RedirectAttributes redirectAttributes, 
                    Model model) throws IOException {

    List<Category> categories = categoryRepo.findAll();

    if (bindingResult.hasErrors()) {
        model.addAttribute("categories", categories);
        return "admin/products/add";
    }

    boolean fileOK = false;
    byte[] bytes = file.getBytes();
    String filename = file.getOriginalFilename();
    Path path = Paths.get("src/main/resources/static/media/" + filename);

    if (filename.endsWith("jpg") || filename.endsWith("png") ) {
        fileOK = true;
    }

    redirectAttributes.addFlashAttribute("message", "Product added");
    redirectAttributes.addFlashAttribute("alertClass", "alert-success");

    String slug = product.getName().toLowerCase().replace(" ", "-");

    Products productExists = productRepo.findBySlug(slug);

    if (! fileOK ) {
        redirectAttributes.addFlashAttribute("message", "Image must be a jpg or a png");
        redirectAttributes.addFlashAttribute("alertClass", "alert-danger");
        redirectAttributes.addFlashAttribute("product", product);
    }
    else if ( productExists != null ) {
        redirectAttributes.addFlashAttribute("message", "Product exists, choose another");
        redirectAttributes.addFlashAttribute("alertClass", "alert-danger");
        redirectAttributes.addFlashAttribute("product", product);
    } else {
        product.setSlug(slug);
        product.setImage(filename);
        productRepo.save(product);
        Files.write(path, bytes);
    }
    return "redirect:/admin/products/add";
}

Solution

You need to add to the model, in the get mapping method, an attribute with name ‘product’ that represents the form. Spring MVC Form tutorial

@GetMapping("/add")
public String add(Model model) {

   List<Category> categories = categoryRepo.findAll();
   model.addAttribute("categories", categories);
   model.addAttribute("product", new Products());
   return "admin/products/add";
}

Answered By – JorgeB

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published