1 Introduction
This is the second part of the Thymeleaf integration with Spring tutorial. You can read the first part here, where you will learn how to configure this project.
As explained at the beginning of the first part of this tutorial, the web application will send two types of requests:
- Insert a new guest: Sends a synchronous request to the server for adding a new guest. This will demonstrate how Thymeleaf is integrated with Spring’s form backing beans.
- List guests: Sends an AJAX request that will update a region (fragment) of the page in order to show the guest list returned from the server.
Let's see how we will accomplish this.
2 Handling forms
In this section we are going to see how a form is submitted with Thymeleaf. We will basically need three attributes:
th:action
th:object
th:field
The first two are defined in the form element:
The th:action attribute rewrites the action url, prefixing the application context to it.
The th:object attribute in the form element is the object selection. It can then be referenced within the form. What we do here is bind the form backing bean to the model attribute, which we defined in the controller before rendering the view:
As we see, th:object refers to the Guest form backing bean, while th:field will refer to its properties. Take a look at the form body:
What th:field will do is assign the
value of its input element to the backing bean property. So, when the user
submits the form, all these th:field will set the form backing bean
properties.
At the controller, we will receive
the Guest instance:
Now the guest can be inserted into the database.
3 Sending AJAX requests
When trying to find a simple example of sending an AJAX request with Thymeleaf, I have found examples with Spring Webflow (render fragments). I also read others saying that you need Tiles in order to accomplish that.
I didn't want to use those frameworks so in this section I'm using jQuery to send an AJAX request to the server, wait for the response and partially update the view (fragment rendering).
The form
This form contains an input text with a search string (searchSurname) that will be sent to the server. There's also a region (resultsBlock div) which will be updated with the response received from the server.
When the user clicks the button, the retrieveGuests() function will be invoked.
The jQuery load function makes a request to the server at the specified url and places the returned HTML into the specified element (resultsBlock div).
If the user enters a search string, it will search for all guests with the specified surname. Otherwise, it will return the complete guest list. These two requests will reach the following controller request mappings:
Since Spring is integrated with Thymeleaf, it will now be able to return fragments of HTML. In the above example, the return string "results :: resultsList" is referring to a fragment named resultsList which is located in the results page. Let's take a look at this results page:
The fragment, which is a table with registered guests, will be inserted in the results block:
4 Conclusion
After integrating both frameworks, we learnt how forms are linked to the Spring MVC model. We also learnt how to send AJAX requests and partially update the view.
I'm publishing my new posts on Google plus and Twitter. Follow me if you want to be updated with new content.
Labels: mongoDB, MVC, Spring, Thymeleaf