Using Apache Tiles in Spring Boot

In my previous tutorial, I showed you how to use Apache Tiles in Spring MVC. I want to write another tutorial on how to use Apache Tiles in Spring Boot to give you an overview of the differences between these two uses. That is the content of this tutorial!

I will use Spring Boot Initilizr Web to create a new Spring Boot project with Web dependency.

Using Apache Tiles in Spring Boot

and import it into the IntelliJ IDE as follows:

Using Apache Tiles in Spring Boot

Now let’s add the dependency of Apache Tiles:

OK, now let’s get started!

The first step, like in Spring MVC, we need files to display the view used by Apache Tiles.

I will retrieve the files in the previous tutorial.
I will create the directory src/main/webapp/WEB-INF/views and copy all the files display the view in the previous tutorial, put into this folder.

Using Apache Tiles in Spring Boot
I will edit the content of the layout.jsp file a bit to fit, specifically as follows:

Since we are using JSTL in the view file, we need to add dependency for the JSTL library:

Next, I will copy the configuration file of Apache Tiles in the previous tutorial and put it in the src/main/webapp/WEB-INF directory.

Using Apache Tiles in Spring Boot

Next we will define the bean for Apache Tiles.
In this step, we will use the @Configuration annotation to define the bean for Apache Tiles. If you do not know how to define bean in Spring’s container using @Configuration annotation, can refer to this tutorial!

I will create a new TilesConfiguration file in the com.huongdanjava.apachetilesspringboot package with following content:

The last step is to define a controller to run.
I will also copy the controller class in the previous tutorial to run

Using Apache Tiles in Spring Boot

OK, so now we can run the application.

One thing to note is that because the default Spring Boot will use Tomcat as a web server to run, we need to add another dependency to support JSP for Tomcat:

Result:

Using Apache Tiles in Spring Boot

Add Comment