Example Maven project with Spring MVC and Apache Tiles 

Joined:
08/13/2009
Posts:
172

January 10, 2014 14:07:20    Last update: January 10, 2014 14:07:20
An example project with Spring MVC and Apache Tiles.
  1. List of files:
    ./pom.xml
    ./src/main/webapp/WEB-INF/web.xml
    ./src/main/webapp/WEB-INF/applicationContext.xml
    ./src/main/webapp/WEB-INF/spring-servlet.xml
    ./src/main/webapp/WEB-INF/jsp/template.jsp
    ./src/main/webapp/WEB-INF/tiles-definitions.xml
    ./src/main/java/com/example/MyController.java
    

  2. ./pom.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>spring.tiles</groupId>
        <artifactId>demo</artifactId>
        <name>spring-tiles</name>
        <packaging>war</packaging>
        <version>1.0</version>
    
        <properties>
    	<java.version>1.6</java.version>
    	<javax.jstl.version>1.2</javax.jstl.version>
    	<org.springframework.version>4.0.0.RELEASE</org.springframework.version>
    	<apache.tiles.version>2.2.1</apache.tiles.version>
    	<org.slf4j.version>1.5.10</org.slf4j.version>
    	<project.build.finalName>springtiles</project.build.finalName>
        </properties>
    
        <dependencies>
    	<dependency>
    	    <groupId>org.springframework</groupId>
    	    <artifactId>spring-webmvc</artifactId>
    	    <version>${org.springframework.version}</version>
    	</dependency>
    
    	<dependency>
    	    <groupId>org.springframework</groupId>
    	    <artifactId>spring-web</artifactId>
    	    <version>${org.springframework.version}</version>
    	</dependency>
    
    	<dependency>
    	    <groupId>org.apache.tiles</groupId>
    	    <artifactId>tiles-core</artifactId>
    	    <version>${apache.tiles.version}</version>
    	</dependency>
    
    	<dependency>
    	    <groupId>org.apache.tiles</groupId>
    	    <artifactId>tiles-jsp</artifactId>
    	    <version>${apache.tiles.version}</version>
    	</dependency>
    
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${org.slf4j.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
                <version>${org.slf4j.version}</version>
                <scope>runtime</scope>
            </dependency>
    
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>${org.slf4j.version}</version>
                <scope>runtime</scope>
            </dependency>
    
    	<dependency>
    	    <groupId>javax</groupId>
    	    <artifactId>javaee-api</artifactId>
    	    <version>6.0</version>
    	    <scope>provided</scope>
    	</dependency>
    
    	<dependency>
    	    <groupId>javax.servlet</groupId>
    	    <artifactId>jstl</artifactId>
    	    <version>${javax.jstl.version}</version>
    	    <scope>runtime</scope>
            </dependency>
        </dependencies>
    
        <build>
    	<plugins>
    	    <plugin>
    		<groupId>org.apache.maven.plugins</groupId>
    		<artifactId>maven-compiler-plugin</artifactId>
    		<configuration>
    		    <source>${java.version}</source>
    		    <target>${java.version}</target>
    		</configuration>
    	    </plugin>
    	</plugins>
    	<finalName>${project.build.finalName}</finalName>
        </build>
    </project>
    

  3. ./src/main/webapp/WEB-INF/web.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
    	http://java.sun.com/xml/ns/javaee 
    	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
        <servlet>
    	<servlet-name>spring</servlet-name>
    	<servlet-class>
    	    org.springframework.web.servlet.DispatcherServlet
    	</servlet-class>
    	<load-on-startup>1</load-on-startup>
        </servlet>
    		
        <servlet-mapping>
    	<servlet-name>spring</servlet-name>
    	<url-pattern>/</url-pattern>
        </servlet-mapping>
    	
        <display-name>Spring Apache Tiles Demo</display-name>
    </web-app>
    

  4. ./src/main/webapp/WEB-INF/applicationContext.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
    	http://www.springframework.org/schema/beans 
    	http://www.springframework.org/schema/beans/spring-beans.xsd">
    </beans>
    

  5. ./src/main/webapp/WEB-INF/spring-servlet.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
    	http://www.springframework.org/schema/beans 
    	http://www.springframework.org/schema/beans/spring-beans.xsd
    	http://www.springframework.org/schema/mvc 
    	http://www.springframework.org/schema/mvc/spring-mvc.xsd
    	http://www.springframework.org/schema/context 
    	http://www.springframework.org/schema/context/spring-context.xsd">
    
    	<context:component-scan base-package="com.example"/>
    
    	<mvc:annotation-driven/>
    
    	<mvc:default-servlet-handler/>
    		
    	<bean id="viewResolver"
    	    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    		<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
    	</bean>
    
    	<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer" id="tilesConfigurer">
    	<property name="definitions">
    	    <list>
    		<value>/WEB-INF/tiles-definitions.xml</value>
    	    </list>
    	</property>
    	</bean>
    </beans>
    

  6. ./src/main/webapp/WEB-INF/tiles-definitions.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE tiles-definitions PUBLIC 
        "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN" 
        "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
    <tiles-definitions>
        <definition name="template" template="/WEB-INF/jsp/template.jsp"/>
    </tiles-definitions>
    

  7. ./src/main/webapp/WEB-INF/jsp/template.jsp:
    <!DOCTYPE html>
    <%@ page pageEncoding="UTF-8" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
    
    <head>
        <title>Tiles Demo</title>
    </head>
    
    <body>
        Tiles Demo
    </body>
    </html>
    

  8. ./src/main/java/com/example/MyController.java:
    package com.example;
    
    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    @Controller
    public class MyController {
    
        @RequestMapping(value="/", method={RequestMethod.GET})
        public String homePage(HttpServletRequest request) {
    	return "template";
        }
    }
    

Share |
| Comment  | Tags