T
T
TonyJa2018-05-30 13:08:10
Java
TonyJa, 2018-05-30 13:08:10

How to display an object for editing on the page?

I use thymeleaf.
In the controller, I pass an object to the page:

@RequestMapping(value = "/products/edit/{id}", method = RequestMethod.GET)
    public String editProduct (@PathVariable Integer id, Model model){
        Product product = service.findById(id);
        model.addAttribute("product", product);
        return "edit";
    }

Product object with 5 fields:
private int id;
    private String name;
    private String description;
    private int price;
    private int stock;

Edit form:
<form action="#" th:action="@{/products/edit/{id}" th:object="${product}" method="post">
    <input type="number" name="id" th:text="*{id}" />
    <input type="text" name="name" th:text="*{name}">
    <input type="text" name="description" th:text="*{description}">
    <input type="number" name="price" th:text="*{price}">
    <input type="number" name="stock" th:text="*{stock}">
    <button type="submit">Edit product</button>
</form>

The form is not working, tell me what needs to be fixed in the form?
I need to put already existing object fields in a form, edit them and submit for update.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
TonyJa, 2018-05-30
@TonyJa

Solved the issue by changing th:text to th:field

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question