Spring MVC 3 Template and Apache Tiles

An efficient design consideration for any web application is the use of a template engine (or tool), and with Spring’s “pluggable” nature, it is indeed much more easier to integrate template mechanisms such as Apache Tiles.

In this simple post, I will give you a brief intro and basics of using Tiles as a Template engine for your Web Application!

  1. Get it Ready: Web Application Setup
  2. Setup Maven and Import the Spring-MVC libraires plus the Apache Tiles
  3. Configuration File
  4. Tiles
  5. Use it!

1st: Web Layout and Application Setup: Get your Web Application Framework ready. For this example, I used Spring 3 MVC with all the minimal components readily injected. Download it here. The project is eclipse ready, so you can just import and load it on your STS (Spring Tool Suite) workspace.

2nd: Setup Maven and generate sources - STS already has a Maven Plugin support. Put a Maven nature first on the project by right clicking on > project > configure > Convert to Maven project.

3rd: POM Configuration – Load the Tiles on the pom.xml. – You need to include the following dependencies to add Apache Tiles libraries to the project.

<!-- For Tiles -->
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-core</artifactId>
<version>2.2.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-template</artifactId>
<version>2.2.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>2.2.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-servlet</artifactId>
<version>2.2.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>

4th: XML Configuration for Class loaded beans - Make sure to setup the tiles xml and call it either directly or from another xml bean configuration file.

5th: Templates – Create the templates.

tiles-definition: – Define the page using the template (mainTemplate.jsp)

mainTemplate.jsp – is the page layout – put the definition attributes.

The registerUser is the page that will be called, the body-position attribute is replaced by a body we defined: jsp/userregistration.jsp

6th: Configure database. Go to data-access-config.xml in your META-INF folder.

SQL Script:

      
delimiter $$
 
CREATE DATABASE `MDCDB` /*!40100 DEFAULT CHARACTER SET latin1 */$$
delimiter $$
CREATE TABLE `MDC_USERS` (
`ID` int(11) unsigned zerofill NOT NULL AUTO_INCREMENT,
`NAME` varchar(45) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1$$

Run the Application!

With the quality and quantity of application development tools, using templates is not new. Creation of these are now strictly mandatory as it will really help the development team to create quality UI faster and better. It also allows developers and designers to work in parallel. Designers using a themeing API, let say JQuery and developers creating the backbone and logic of the application – using EJBs, makes the definition of “ease of development” more apparent.

Download my sample and open it in your STS (Spring Tool Suite) here. Make sure you have the Hibernate and Maven Plugin installed.

Reference: Spring MVC 3 with Template using Apache Tiles from our JCG partner Alvin Reyes at the Alvin “Jay” Reyes Blog blog.

Related Whitepaper:

Introduction to Web Applications Development

Kick start your web apps development with this introductory ebook!

This 376 page eBook 'Introduction to Web Applications Development', starts with an introduction to the internet, including a brief history of the TCT/IP protocol and World Wide Web. It defines the basic concepts for web servers and studies the case of Apache, the most used webserver, while other free software webservers are not forgotten. It continues with webpage design focusing on HTML and JavaScript. XML Schemas, their validation and transformation are covered as well as dynamic webpages built with CGI, PHP or JSP and database access.

Get it Now!  

Leave a Reply


+ 4 = nine



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books