Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC 3.2 Thymeleaf Ajax Fragments

I'm building application with Spring MVC 3.2 and Thymeleaf templating engine. I'm a beginner in Thymeleaf.

I have everything working, including Thymeleaf but I was wondering if anyone knows of a simple and clear toturial on how to do simple Ajax request to controller and in result rendering only a part of a template (fragment).

My app has everything configured (Spring 3.2, spring-security, thymeleaf, ...) and works as expected. Now I would like to do Ajax request (pretty simple with jQuery but I don't wan't to use is since Thymeleaf in its tutorial, chapter 11: Rendering Template Fragments (link) mentiones it can be done with fragments.

Currently I have in my Controller

@RequestMapping("/dimensionMenuList") public String showDimensionMenuList(Model model) {      Collection<ArticleDimensionVO> articleDimensions;     try {         articleDimensions = articleService.getArticleDimension(ArticleTypeVO.ARTICLE_TYPE);     } catch (DataAccessException e) {         // TODO: return ERROR         throw new RuntimeException();     }      model.addAttribute("dimensions", articleDimensions);      return "/admin/index :: dimensionMenuList"; } 

the part of the view where I would like to replace <ul></ul> menu items:

<ul th:fragment="dimensionMenuList" class="dropdown-menu">     <li th:unless="${#lists.isEmpty(dimensions)}" th:each="dimension : ${dimensions}">         <a href="#" th:text="${dimension.dimension}"></a>     </li> </ul> 

Any clue is greatly appreciated. Especially if I don't have to include any more frameworks. It's already too much for java web app as it is.

like image 493
y0gie007 Avatar asked Jan 07 '14 21:01

y0gie007


People also ask

How do I use Ajax with Thymeleaf?

Make an ajax call using the url defined in the form's th:action. Serialize the form data. Your controller will be able to recieve this in an object. Replace the part of your html code with the returned fragment.

Is Thymeleaf supported by Spring MVC?

Thymeleaf offers a set of Spring integrations that allow you to use it as a fully-featured substitute for JSP in Spring MVC applications.

How do I use Thymeleaf fragments?

There are three basic ways to include content from a fragment: insert – inserts content inside the tag. replace – replaces the current tag with the tag defining the fragment. include – this is deprecated but it may still appear in a legacy code.

What is Thymeleaf template?

Thymeleaf is a modern server-side Java template engine for both web and standalone environments, capable of processing HTML, XML, JavaScript, CSS and even plain text. The main goal of Thymeleaf is to provide an elegant and highly-maintainable way of creating templates.


2 Answers

Here is an approach I came across in a blog post:

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

<form>     <span class="subtitle">Guest list form</span>     <div class="listBlock">         <div class="search-block">             <input type="text" id="searchSurname" name="searchSurname"/>             <br />             <label for="searchSurname" th:text="#{search.label}">Search label:</label>             <button id="searchButton" name="searchButton" onclick="retrieveGuests()" type="button"                      th:text="#{search.button}">Search button</button>         </div>          <!-- Results block -->         <div id="resultsBlock">          </div>     </div> </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.

function retrieveGuests() {     var url = '/th-spring-integration/spring/guests';      if ($('#searchSurname').val() != '') {         url = url + '/' + $('#searchSurname').val();     }      $("#resultsBlock").load(url); } 

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:

@RequestMapping(value = "/guests/{surname}", method = RequestMethod.GET) public String showGuestList(Model model, @PathVariable("surname") String surname) {     model.addAttribute("guests", hotelService.getGuestsList(surname));      return "results :: resultsList"; }  @RequestMapping(value = "/guests", method = RequestMethod.GET) public String showGuestList(Model model) {     model.addAttribute("guests", hotelService.getGuestsList());      return "results :: resultsList"; } 

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:

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"     xmlns:th="http://www.thymeleaf.org" lang="en">  <head> </head>  <body>     <div th:fragment="resultsList" th:unless="${#lists.isEmpty(guests)}" id="results-block">         <table>             <thead>                 <tr>                     <th th:text="#{results.guest.id}">Id</th>                     <th th:text="#{results.guest.surname}">Surname</th>                     <th th:text="#{results.guest.name}">Name</th>                     <th th:text="#{results.guest.country}">Country</th>                 </tr>             </thead>             <tbody>                 <tr th:each="guest : ${guests}">                     <td th:text="${guest.id}">id</td>                     <td th:text="${guest.surname}">surname</td>                     <td th:text="${guest.name}">name</td>                     <td th:text="${guest.country}">country</td>                 </tr>             </tbody>         </table>     </div> </body> </html> 

The fragment, which is a table with registered guests, will be inserted in the results block.

like image 189
Sohail Avatar answered Sep 30 '22 07:09

Sohail


Rendering only Thymeleaf fragments also works well with ModelAndView.

Your controller

@RequestMapping(value = "/feeds", method = RequestMethod.GET) public ModelAndView getFeeds() {     LOGGER.debug("Feeds method called..");     return new ModelAndView("feeds :: resultsList"); } 

Your view

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">  <head></head> <body>     <div th:fragment="resultsList" id="results-block">         <div>A test fragment</div>         <div>A test fragment</div>     </div> </body> </html> 

What's actually rendered

<div id="results-block">     <div>A test fragment</div>     <div>A test fragment     </div> </div> 
like image 44
Abdullah Khan Avatar answered Sep 30 '22 06:09

Abdullah Khan