Skip to main content

Spring Boot, Thymeleaf static resource refresh

Thymeleaf is a natural template engine that is often used in modern java-server-side applications that are running in web and standalone environments. Ease of natural templating gives the upperhand to pick Thymeleaf over other frameworks in Java Serverside application development. Spring provides support to thmyeleaf development. Spring boot has its own starter dependency for thymeleaf. While we develop thmyeleaf templates using spring boot applications, Front End Developers cannot see the changes that are made in the resources while the application is running. But the changes don't take effect in the browser until the application is reloaded or restarted.

We can solve this templating-resources-reloading without application restart. Spring boot uses properties in which we have to just disable the cache for Thymeleaf. Below are the steps to achieve these.

Step 1: Disable the cache by setting the spring.thymeleaf.cache = false in the file. 
The property just disables the Thmyeleaf caching template in Spring applications. If you are working on different(dev, prod, staging) environments, make sure you are updating relevant environment properties so that your production environment still caches the Thymeleaf template.

Step 2: We just need to update pom.xml and set spring-boot-maven-plugin 'addResources' configuration to true.
When addResources is enabled, any src/main/resources folder will be added to the application classpath when you run the application and any duplicate found in target/classes will be removed. This allows hot refreshing of resources and ensures the application picks from resources folder rather than the classpath resources.

Assumption      : Spring Boot (1.5.6),  Thymeleaf (3.0.6), Maven Projects


Popular posts from this blog

CSRF enabled Ajax requests using Spring Security

Many of you who have worked on Spring Security might be aware of the fact that Spring Security protects applications from Cross Site Request Forgery using _csrf tokens in the request sent to the web server. You can find a detailed understanding in the Spring documentation page. The objective of this post is to explain how to send _csrf tokens in the Ajax requests when we protect our application URL and application access using spring security.

How to get CSRF tokens
While we submit a form using an application that is protected with Spring Security, the form gets a default hidden parameter in the form body when using <form:form> element. The param contains the _csrf tokens to authenticate the requests in the server. In case we use other ways to create forms, we have to manually include a hidden parameter that contains name as ${_csrf.parameterName} and  value as ${_csrf.token}. For example,

In-Place editing with X-Editable using Datatable plugin

In-place editing is a trending feature that can be seen in many latest web applications, a popular example would be where the editing data happens on the web page without any explicit forms or popups. Another such example I could point out is, The in-place editing is very nice in such a way that editing data seems so natural and user friendly.

To enable in-place editing, there are many free JQuery plugins available on the internet. We are discussing a very popular plugin called x-editable. Most of the time we use html tables to display data where in-place editing is enabled. Hence we need another plugin to elegantly display tables with enormous data. We use a famous JQuery plugin called datatable. Both of these plugins are used widely and free to use.

Assumption      : Bootstrap 3, JQuery used Projects

In order to enable datatable features on any ordinary table found on web page, we should initialise datatable plugin for that table. I…

Thymeleaf th:remove vs th:if Processors

Interestingly Makinus team encountered a scenario where the team has to choose between th:remove and th:if processors. The situation is like this. Makinus team worked on a project and the team lead suggested to go with #Thymeleaf template instead of decades old JSP. Hence it adopted Thymeleaf and Thymelead Layout framework to do layout-ing.

Thymeleaf Layout
Thymeleaf layout is a framework that helps doing the layout work just as tiles framework does it using JSP technology. The framework has 5 processors, important of which is layout:decorate. The processor layout:decorate works in the layout-content template model where the content template is a small piece of template that replaces content part of the layout template. This works as if parent/child templating mode where parent will define layout format while the child template replaces only the specified content portions.

Head Element Merging Strategy
Thymeleaf layout uses a special strategy to merge parent and child template <head…