Skip to content
Advertisement

How to store Form data in a H2 Database (currently works using POSTman, but not the website form)

I am just teaching myself the Spring framework and am trying to makie a simple library app which stores user (book) inputs in a form in the H2 Database, and then shows the user the newly inputted data from the H2 Database.

Form submission attempt 1:

enter image description here

I now have a working POST method which correctly saves a new book to the H2 repository – however when I try to connect this method to the submission of a form, no such repository stores occur.

Form submission attempt 2:

enter image description here

Working POST request using POSTman 3:

enter image description here

Working POST request using POSTman 4:

enter image description here

Code snippets below:

HTMLfile with the Thymeleaf form inside:

JavaScript

POST method in the controller class which works for POST requests from POSTman, but fails to store the form data in the same way:

JavaScript

Basket @entity:

JavaScript

Advertisement

Answer

For anyone wondering, this is how you do it…

Set the form up like below, (it just needs to point to the name of the path in the controller method):

JavaScript

Then set up the method in the controller like so:

JavaScript

^It takes in the inputs from the form as a Basket object so you just need to save this to the appropriate repository, make sure you have a toString() method in this Basket class so you can print out the results if necessary to do so

This is my Basket class in case this is useful:

JavaScript

^it has the same variable names as those used in the HTML file.

When you run your locally hosted site, if you have an appropriate getter for basket e.g

JavaScript

… then you should be able to see your newly stored basket object in your basket repository.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement