Book Image

SPRING COOKBOOK

Book Image

SPRING COOKBOOK

Overview of this book

Table of Contents (19 chapters)
Spring Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using a page template with Tiles


With a page template, avoid repeating the common elements of the pages (HTML head, header, footer, navigation, and so on) in every JSP.

How to do it…

Here are the steps to use Tiles:

  1. Add the Tiles Maven dependencies in pom.xml:

    <dependency>
      <groupId>org.apache.tiles</groupId>
      <artifactId>tiles-servlet</artifactId>
      <version>3.0.5</version>
    </dependency>
    
    <dependency>
      <groupId>org.apache.tiles</groupId>
      <artifactId>tiles-jsp</artifactId>
      <version>3.0.5</version>
    </dependency> 
  2. In the Spring configuration class, remove the JSP view resolver (if it's there).

  3. Declare Tiles in the Spring configuration class:

    // declare Tiles configuration file
    @Bean
    public TilesConfigurer tilesConfigurer() {
      TilesConfigurer tilesConfigurer = new TilesConfigurer();
      final String[] definitions = { "/WEB-INF/tiles.xml" };
        tilesConfigurer.setDefinitions(definitions);
      return...