Using Apache Tiles in Spring MVC

Apache Tiles is an open source template engine used for web applications in Java. It allows us to reuse common elements between pages in our web application. In this tutorial, I will guide you to use Apache Tiles in your Spring MVC!

I will create a Spring MVC project as follows:

Using Apache Tiles in Spring MVC

If someone does not know how to create Spring MVC project, you can refer to this tutorial!

Here, I upgraded the libraries version to the latest version.

Next, I will add the dependency of Apache Tiles to the project.

Before I start, I will edit the home.jsp file in the src/main/webapp/WEB-INF/views folder so you can easily imagine the benefits of using Apache Tiles.

The contents of my home.jsp file are now as follows:

Now, I will run this application using the Maven Jetty Plugin, resulting in the following:

Using Apache Tiles in Spring MVC

OK, now I’m starting to use Apache Tiles!

The requirement of the application is that we keep only the “Apache Tiles and Spring Integrations !!!” and the word “Copyright by Huong Dan Java”, while the content can change between the pages together. So what do we do now?

There are three steps we need to do in order to be able to use Apache Tiles in our application:

The first step is to define the layout for Apache Tiles.

To do this, I will first edit the view directory in my project a little. I will create a new file named layout.jsp located in the src/main/webapp/WEB-INF/views folder with content similar to the content of the home.jsp file but with editing to use the properties of Apache Tiles. The contents of the layout.jsp file are as follows:

In the layout.jsp file, if you notice, you cal see that I have declared a taglib of Apache Tiles to be able to use the <tiles:insertAttribute> tag. This tab works depending on the page, the content of the file is declared with the name as the “content” that will be used to display the interface.

The layout.jsp file is like a frame depending on the page that will have different content but still have some same parts.

The home.jsp file, I will edit its content as follows:

OK, now I’m going to create a file to configure the layout for Apache Tiles. Create a file named tiles.xml located in the src/main/webapp/WEB-INF folder.

Using Apache Tiles in Spring MVC

And add the content for the tiles.xml file as follows:

You see, in this file I have defined a baselayout, which is the content of the layout.jsp file. And our home page will extends from this baselayout, with the content of the “content” attribute being derived from the contents of the home.jsp file.

Easy to understand, right?

OK, now we will move on to the second step.

In this second step, we will declare an Apache Tiles bean to load the configuration we just defined in the tiles.xml file.

Open the servlet-context.xml file and add the following lines:

The final step is to replace the default ViewResolver with Apache Tiles ViewResolver.

You also open the servlet-context.xml file and replace:

to

OK, so we finished using Apache Tiles in Spring MVC. Now, if you run again you will also see the same interface.

Using Apache Tiles in Spring MVC

Now, to add a new login page, I just follow these steps:

– Add a request URL in the HomeController class.

– Add the login.jsp file in the src/main/webapp/WEB-INF/views directory with the following content:

– Add configuration to login page in Apache Tiles configuration file:

Result:

Using Apache Tiles in Spring MVC

Add Comment