Spring MVC 3.1 and Apache Tiles 2 maven archetype

Find in this post a Maven archetype for a Spring MVC 3.1 and Apache tiles 2.
The generated application is maven-based web application (.war) where the dependencies are all configured and set:

  • servlet-api 2.5
  • Apache tiles 2.2
  • Spring MVC 3.1
  • jstl 1.2
  • Java 1.6 version (this is not a constraint, I think you could downgrade the version to 1.5 without difficulties)

The application is ready to deploy on a web application server (tested on Apache tomcat 6.x and higher).

Installing and using the archetype

  1. Download the archetype from this link
  2. Install the downloaded archetype to your local repository using the following command (sorry it is not yet available from the Maven central)
    mvn install:install-file -Dfile=springmvc3.1-archetype-1.0.1.jar -DgroupId=com.michir.projects -DartifactId=springmvc3.1-archetype -Dversion=1.0.1 -Dpackaging=jar -DgeneratePom=true [-DlocalRepositoryPath=[path to your local repository]]
  3. Once installed on your local repository, generate a new web project using the following maven command
    mvn archetype:generate -DarchetypeGroupId=com.michir.projects -DarchetypeArtifactId=springmvc3.1-archetype -DarchetypeVersion=1.0.1 -DgroupId=com.mycompany -DartifactId=myproject -Dversion=1.0.0-SNAPSHOT

A new (Maven) web project is generated under the “myproject” folder (your artifactId from the maven command line). The projects comes with:

  • A spring configuration file under src/main/webapp/WEB-INF/spring-servlet.xml with an Apache tiles 2 configuration, pointing to tiles.xml tiles configuration file (see Spring MVC documentation for more View Resolvers).
  • The web application is configured (see web.xml) to map the Spring Dispatcher Servlet to *.html
  • The spring framework will scan the package com.mycompany (your groupId from the maven command line) for spring annotated controllers, see the “context:component-scan” tag in spring-servlet.xml file
  • The application comes with Page1Controller.java in the package “com.mycompany” spring controller (as an example) that maps page1.html, it is redirected to the page1.jsp view under src/main/webapp/WEB-INF/jsp/

Adding a new controller, a new view to the project

If your are not used with Spring MVC framework and Apache tiles, adding a new component may seem tedious. Here are the steps to achieve that (base on the above installation and configuration):

  1. Create a new Spring @Controller, say MayController.java in the package “com.mycompany” that maps, say “page2.html”
    package com.mycompany;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    public class MyController {
    	public String redirect() {
    		return "page2";
  2. Create a “page2.jsp” file in “WEB-INF/jsp” folder, along with “page1.jsp”:
    <!-- to use spring jsp tags -->
    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
        <title>Page 2</title>
        <h2>Page 2</h2>
  3. Add the “page2.jsp” definition to the “tiles.xml” file:

    <definition name="page2" extends="base.definition">
       <put-attribute name="title" value="Page 2" />
       <put-attribute name="body" value="/WEB-INF/jsp/page2.jsp" />

It is done, you can now compile/build your application, deploy on a web server and test it (oh … perhaps one last thing, add a link to your newly added page in menu.jsp along with the “page1” link like this:

   <a href="page1.html">Page 1</a><br />
   <a href="page2.html">Page 2</a><br />