record is not added to the table

Dr.Joe :

I want to save a new record to the table - Body. I use Spring Boot and HTML. so it looks in HTML:

<select id="bodiesList" multiple="multiple" class="selector">
    <option th:each="b : ${bodies}"
    th:value="${b.id}"
    th:text="${b.name}"></option>
</select>

Controller:

@Controller
@RequestMapping("/general")
public class OrderController {

    @Autowired
    @Qualifier("orderServiceImpl")
    private OrderService orderService;

    @GetMapping(value = "/list")
    public String get(Model theModel) {

        theModel.addAttribute("orders", orderService.getOrders());

        return "general/index";
    }

    @GetMapping(value = "/showFormForAdd")
    public String add(Model theModel) {
        Orders orders = new Orders();

        List<Body> body = orderService.getBody();

        theModel.addAttribute("bodies", body);
        theModel.addAttribute("order", orders);

        return "general/create-orders";
    }

    @PostMapping(value = "/saveOrders")
    public String addOrders(@ModelAttribute("order") Orders orders, @RequestParam("picture") MultipartFile file) throws IOException {

        orders = orderService.uploadOrders(orders, file);
        orderService.save(orders);

        return "redirect:/general/list";
    }

}

but not in the database not in my column nothing is saved.

enter image description here

what needs to be fixed in my code?

Sumit :

Use th:field to send the values from your html page to your controller. try something like this -

 <select th:field="*{body}" id="bodiesList" multiple="multiple" class="selector">
                    <option th:each="b : ${bodies}"
                            th:value="${b.id}"
                            th:text="${b.name}"></option>
                </select>

And then capture this information on your controller after form submit (same as your're capturing the orders on your saveOrder controller.

Read more about th:field in thymelead official documentation here - https://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=25362&siteId=1