Bronwyn Young
Bronwyn Young

Reputation: 33

Spring Thymeleaf Html form + Crud Operations

I'm new to Spring Framework and currently trying develop an simple application were restaurant owners can add a dish to a main database through logging in and imputing the dish data into a Html form (with Thymeleaf). (dish name, price, gluten free = true etc.)

App users should then be able to search the main database based on their location and dietary requirements etc.

I'm trying to learn through Youtube tutorials however, all of them seem to use @RestController and @Requestbody and use Postman to send JSON requests. I'm having difficulty adapting this to my Html form.

I have used the @controller annotation for simple 'save' methods but when I research 'how to include a foreign key in the data added to the data base' (i.e. include 'restaurant id' as foreign key in the 'dish' class), all of the tutorials use @RequestController and JSON requests in Postman.

As I'm trying to build a usable application sending requests in Postman doesn't seem like a good solution.

I think the problem I'm according to other posts is the following:

"If you use template engine like Thymeleaf it will not work with @RestController because of @ResponseBody which included in this annotation" – Sonique

"@ResponseBody makes the returned objects to something that could be in the body, e.g. JSON or XML"  – Martin Thoma

I've tried removing @RequestBody and changing it to @RequestParam but I get errors.

Are there any work-arounds to my issue? E.g. using a different file format for the 'add new dish' form or not using Thymeleaf?

Any help/advice would be very much appreciated!

Upvotes: 0

Views: 95

Answers (1)

BASSAM ALHARBI
BASSAM ALHARBI

Reputation: 69

If you want to use HTML with Thymeleaf and the class should annotate by @Controller and the method parameter that handle the object from the FORM you want to control should be @ModelAttribute("").

Upvotes: 1

Related Questions