Tiles and Spring

Spring 3 and Tiles 2 Integration

Podcastpedia.org makes use of Apache Tiles for its layout. This approach was taken because the website pages share a similar structure. Please read first the Getting started guide and Tiles concepts to have a better understanding of what follows. This post will present how Tiles is integrated with Spring MVC for Podcastpedia.org

Note: Spring is the basic technology used for developing Podcastpedia.org

Necessary artifacts

First of all Tiles jars are required in the classpath. They can be directly downloadded from the official website, but Tiles has also been published to the public Maven repository.

You can use one dependency to download all Tiles supported technologies with the following dependency declaration:


The basic Tiles dependency with only servlet support can be added this way:

If you need a dependency to Tiles JSP support, Declare the dependency this way:

Spring application context configuration for Tiles

For better readability the Spring Tiles configuration has been placed in a separate file pcm-tiles.xml :

Of interest are

  • the TilesConfigurer (line 53) entry bean, which simply configures a TilesContainer using a set of files containing definitions (templates.xml and definitions.xml), to be accessed by TilesView instance. This is a Spring-based alternative (for usage in Spring configuration) to the Tiles-provided TilesListener (for usage in web.xml)
  • the tilesViewResolver instance (line 37) which is a org.springframework.web.servlet.View implementation that retrieves the Tiles definitions. The order property and the contentNegotiatingResolver will be discussed in another post.

Creating and using Tiles pages

After installing and learning some of Tiles concepts, it’s time to show you how the pages are created for Podcastpedia.org

Create a template

Podcastpedia.org uses sort of a classic  layout page structure:

Template

Template

As you can see the layout of the website is made of

  • header which includes the social media connect, language selection logo and search bar
  • navigation bar (menu)
  • content – bulk data (body)
  • footer which includes links to Contact, About Us, Disclaimer pages

The first step was to create a JSP page that acts as this layout and place it under /WEB-INF/template/template.jsp file:

You can see highlighted the four attributes  header, navigation_bar (menu), content (body) and footer, which resemble the template layout from the picture.  Besides these there are still some other Tiles attributes, like title, page_description or og_image,  that will get filled when rendering actual pages.

Create the composing pages

In this phase I had to create four JSP pages, that will take place of header, navigation_bar (menu), content (body) and footer attributes in the previously created template.

Create a definition

A definition is a composition to be rendered to the end user; essentially a definition is composed of a template and completely or partially filled attributes.

  • If all of its attributes are filled, it can be rendered to the end user.
  • If not all of its attributes are filled, it is called an abstract definition, and it can be used as a base definition for extended definitions, or their missing attributes can be filled at runtime.

Initially I created a /WEB-INF/tile-defs/template.xml, which acts as an abstract definition and is extended by all the other Tiles definitions across the Podcastpedia application:

Now let’s see some examples of definitions extending the defaultTemplate one defined above. For example the definition rendering the home page  of Podcastpedia.org looks like the following:

Note that the Tiles attributes are filled with static values specific to the start page, and there are some attributes from the defaultTemplate (like header and footer) that are not overriden – that means they will be inherited from the defaultTemplate (in the end reuse is one of the advantages of inheritence).

Let’s see another example. This definition – podcastDetails – is used to render a podcast details page :

Notice here you can also use dynamic values for the Tiles attributes – for example the title attribute (line 4) is set dynamically with a value,(${podcast.title}), passed from the controller . If you follow the link – The Naked Scientist Podcast – and inspect the source code or the tab name in your browser, you will notice the title of the page was set to “- The Naked Scientists Podcast – Stripping Down Science”

Well, this is pretty much all you need to do to integrate Tiles 2 in Spring MVC.

If you liked this, please show your support by helping us with Podcastpedia.org
We promise to only share high quality podcasts and episodes.

Resources

  1. http://tiles.apache.org/
  2. http://www.springsource.org/
Podcastpedia image

Adrian Matei

Founder of Podcastpedia.org and Codingpedia.org, computer science engineer, husband, father, curious and passionate about science, computers, software, education, economics, social equity, philosophy.

Adrian’s favorite Spring and Java books

About Adrian Matei

Creator of Podcastpedia.org and Codingpedia.org, computer science engineer, husband, father, curious and passionate about science, computers, software, education, economics, social equity, philosophy.

  • Pingback: Upgrade from Tiles 2 to Tiles 3 on Spring MVC 3 application | Codingpedia.org

  • Pingback: Spring MVC and Tiles integration example | Codingpedia.org

  • Pingback: SEO: Dynamic title and meta description with Tiles and Spring MVC | Codingpedia.org

  • Satish Kadyan

    can you please provide the link with code of project developed with above tutorial. your tutorial seems very interesting, but one problem is that beginner faces problem in binding all discussion to add full project code. it is dificult to implement by beginner programmer.

    • http://www.codingpedia.org/ Adrian Matei

      Hi Satish,

      Currently there’s no link to the code of the project, but Podcastpedia.org will become open-source really soon. Hang in there :)
      Adrian

  • Micheal Nghe

    Hi,

    I have followed your post, but I don’t know why the dynamic title passing from controller does not replace in template

    tiles

    @controller
    model.addAttribute(“title”, “Test Title”);

    • http://www.codingpedia.org/ Adrian Matei

      HI Micheal,

      Did you add all the dependencies? I tried the same as you and it works for me…

      and in the controller