Search code examples
spring-mvcobjectspring-bootpostthymeleaf

SpringBoot MVC - Post ThymeLeaf Object


I was wondering whether or not it would be possible to POST an object using ThymeLeaf and SpringBoot?

I have looked online but it doesn't seem to be very well documented.

I know the below code doesn't work but it shows the general gist of what I want to achieve.

Please assume that there is an object passed into the view called "part" of type Part.

The Controller:

@Controller
@RequestMapping("/basket")
public class BasketController {
@RequestMapping(value = "/add", method = RequestMethod.POST)

public ModelAndView addToBasket(@ModelAttribute("part") Part part) {

 ModelAndView mv = new ModelAndView("basket/viewBasket");
 return mv;

}
}

And the view:

<form method="POST" th:action="@{/basket/add}" th:object="${part}" modelAttribute="part">
                <input th:object="${part}"/>
                <input type="submit" class="btn btn-warning btn-block" th:value="#{basket.add}"/>
            </form>

Thanks!


Solution

  • In the end I created some hidden fields and posted the relevant information.

    The object was then re-created server side.

    Thanks for the help!