title: Spring mvc integration with Apache Tiles 3 Using Java Configuration has Problems
body: <p><strong>The problem is the that tiles configuration is not getting applied 
  everytime.</strong>
  I have integrated Apache tiles 3 with spring MVC. I have used annotation based configuration of spring. The problem in the application is that the tiles definitions get applied on random basis. When I try to run the application, the tiles configuration that we have configured may or may not get applied.</p>
  
  <p>I am using Apache tomcat 7. Is this problem related with server? Is this problem related with Configuration? Or If Any. </p>
  
  <p>Here is my Code</p>
  
  <p><strong>MVC Configuration</strong></p>
  
  <pre><code>        Annotation base Java Configuration using Spring
          @EnableWebMvc
  
          @Configuration
  
          @ComponentScan(basePackages = { "com.om.*" })
  
          public class MVCConfig extends WebMvcConfigurerAdapter {
  
  
              @Override
              public void addResourceHandlers(ResourceHandlerRegistry registry) {
                  registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
              }
  
  
              @Override
              public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
                  configurer.enable();
              }
              @Bean
              public InternalResourceViewResolver viewResolver() {
                  InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
                  viewResolver.setViewClass(JstlView.class);
                  viewResolver.setPrefix("/WEB-INF/views/layout/");
                  viewResolver.setSuffix(".jsp");
                  return viewResolver;
              }
  
  
              @Bean
              public TilesViewResolver getTilesViewResolver() {
                  TilesViewResolver tilesViewResolver = new TilesViewResolver();
                  tilesViewResolver.setViewClass(TilesView.class);
                  return tilesViewResolver;
              }
              @Bean
              public TilesConfigurer getTilesConfigurer() {
                  TilesConfigurer tilesConfigurer = new TilesConfigurer();
                  tilesConfigurer.setCheckRefresh(true);
                  tilesConfigurer.setDefinitionsFactoryClass(TilesDefinitionsConfig.class);
  
                  // Add apache tiles definitions
                  TilesDefinitionsConfig.addDefinitions();
  
                  return tilesConfigurer;
              }
          }
  </code></pre>
  
  <p><strong>MVCInitializer</strong> </p>
  
  <pre><code>        public class MVCInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
  
              @Override
              protected Class&lt;?&gt;[] getRootConfigClasses() {
                  return new Class[] { MVCConfig.class };
              }
  
              @Override
              protected Class&lt;?&gt;[] getServletConfigClasses() {
                  return null;
              }
  
              @Override
              protected String[] getServletMappings() {
                  return new String[] { "/" };
              }
  
  
          }
  </code></pre>
  
  <p><strong>TilesDefinitionsConfig</strong> </p>
  
  <pre><code>        public final class TilesDefinitionsConfig implements DefinitionsFactory {
  
              private static final Map&lt;String, Definition&gt; tilesDefinitions = new HashMap&lt;String,Definition&gt;();
              private static final Attribute BASE_TEMPLATE = new Attribute("/WEB-INF/views/layout/defaultLayout.jsp");
  
              public Definition getDefinition(String name, Request tilesContext) {
                  System.out.println("3");
                  return tilesDefinitions.get(name);
              }
  
              /**
               * @param name &lt;code&gt;Name of the view&lt;/code&gt;
               * @param title &lt;code&gt;Page title&lt;/code&gt;
               * @param body &lt;code&gt;Body JSP file path&lt;/code&gt;
               *
               * &lt;code&gt;Adds default layout definitions&lt;/code&gt;
               */
              private static void addDefaultLayoutDef(String name, String title, String body) {
                  Map&lt;String, Attribute&gt; attributes = new HashMap&lt;String,Attribute&gt;();
  
                  attributes.put("title", new Attribute(title));
                  attributes.put("header", new Attribute("/WEB-INF/views/layout/header.jsp"));
                  attributes.put("menu", new Attribute("/WEB-INF/views/layout/menu.jsp"));
                  attributes.put("body", new Attribute(body));
                  attributes.put("footer", new Attribute("/WEB-INF/views/layout/footer.jsp"));
  
                  tilesDefinitions.put(name, new Definition(name, BASE_TEMPLATE, attributes));
              }
  
              /**
               * &lt;code&gt;Add Apache tiles definitions&lt;/code&gt;
               */
              public static void addDefinitions(){
  
                  addDefaultLayoutDef("welcome", "welcome", "/WEB-INF/views/layout/welcome.jsp");
                  addDefaultLayoutDef("personList", "viewPerson", "/WEB-INF/views/layout/personList.jsp");
  
              }
          }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>        @Controller
  
          public class SpringTilesController {
  
  
              @RequestMapping(value="welcome")
              public ModelAndView index() {
  
                  ModelAndView model=new ModelAndView();
                  System.out.println("In Controller");
                  model.setViewName("welcome");       
                  return model;
              }
  
              @RequestMapping(value="viewPerson")
              public ModelAndView viewPersons(Model model) {
                  Map&lt;String, List&lt;Person&gt;&gt; persons = 
                          new HashMap&lt;String, List&lt;Person&gt;&gt;();
                  persons.put("persons", Person.createPersons());
                  return new ModelAndView("personList", persons);
              }
          }
  </code></pre>
  
  <p><strong>Entity</strong></p>
  
  <pre><code>        public class Person {
  
              private String name, email;
              private int age;
  
              public Person(String name, String email, int age) {
                  this.name = name;
                  this.email = email;
                  this.age = age;
              }
  
              public String getName() {return name;}
              public void setName(String name) {this.name = name;}
  
              public String getEmail() {return email;}
              public void setEmail(String email) {this.email = email;}
  
              public int getAge() {return age;}
              public void setAge(int age) {this.age = age;}
  
              @Override
              public String toString()
              { 
                  return String.format(
                      "Person [name = %s, email = %s, age = %d]", 
                          name, email, age);
              }
  
              public static List&lt;Person&gt; createPersons() {
                  List&lt;Person&gt; persons = new ArrayList&lt;Person&gt;();
                  persons.add(new Person("Tousif", "tousif@mail.com", 32));
                  persons.add(new Person("Asif", "asif@mail.com", 28));
                  persons.add(new Person("Ramiz", "ramiz@mail.com", 26));
                  persons.add(new Person("Rizwan", "rizwan@mail.com", 32));
                  persons.add(new Person("Amol", "amol@mail.com", 33));
                  persons.add(new Person("Ramdas", "ramdas@mail.com", 31));
                  return persons;
              }
          }
  </code></pre>
  
  <p><strong>pom.xml</strong></p>
  
  <pre><code>        &lt;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"&gt;
              &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
              &lt;groupId&gt;com.om&lt;/groupId&gt;
              &lt;artifactId&gt;TilesDemo&lt;/artifactId&gt;
              &lt;packaging&gt;war&lt;/packaging&gt;
              &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
              &lt;name&gt;TilesDemo Maven Webapp&lt;/name&gt;
              &lt;url&gt;http://maven.apache.org&lt;/url&gt;
  
              &lt;properties&gt;
                  &lt;jdk.version&gt;1.7&lt;/jdk.version&gt;
                  &lt;spring.version&gt;4.1.6.RELEASE&lt;/spring.version&gt;
                  &lt;spring.security.version&gt;4.0.1.RELEASE&lt;/spring.security.version&gt;
                  &lt;jstl.version&gt;1.2&lt;/jstl.version&gt;
                  &lt;javax.servlet.version&gt;3.1.0&lt;/javax.servlet.version&gt;
                  &lt;mysql.connector.version&gt;5.1.31&lt;/mysql.connector.version&gt;
              &lt;/properties&gt;
  
  
              &lt;dependencies&gt;
                  &lt;dependency&gt;
                      &lt;groupId&gt;junit&lt;/groupId&gt;
                      &lt;artifactId&gt;junit&lt;/artifactId&gt;
                      &lt;version&gt;3.8.1&lt;/version&gt;
                      &lt;scope&gt;test&lt;/scope&gt;
                  &lt;/dependency&gt;
                  &lt;!-- jstl for jsp page --&gt;
  
  
                  &lt;dependency&gt;
                      &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
                      &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
                      &lt;version&gt;3.1.0&lt;/version&gt;
                  &lt;/dependency&gt;
  
                  &lt;dependency&gt;
                      &lt;groupId&gt;jstl&lt;/groupId&gt;
                      &lt;artifactId&gt;jstl&lt;/artifactId&gt;
                      &lt;version&gt;${jstl.version}&lt;/version&gt;
                  &lt;/dependency&gt;
  
                  &lt;!-- Apache Tiles --&gt;
                  &lt;dependency&gt;
                      &lt;groupId&gt;org.apache.tiles&lt;/groupId&gt;
                      &lt;artifactId&gt;tiles-api&lt;/artifactId&gt;
                      &lt;version&gt;3.0.5&lt;/version&gt;
                      &lt;scope&gt;compile&lt;/scope&gt;
                  &lt;/dependency&gt;
                  &lt;dependency&gt;
                      &lt;groupId&gt;org.apache.tiles&lt;/groupId&gt;
                      &lt;artifactId&gt;tiles-core&lt;/artifactId&gt;
                      &lt;version&gt;3.0.5&lt;/version&gt;
                      &lt;scope&gt;compile&lt;/scope&gt;
                  &lt;/dependency&gt;
                  &lt;dependency&gt;
                      &lt;groupId&gt;org.apache.tiles&lt;/groupId&gt;
                      &lt;artifactId&gt;tiles-jsp&lt;/artifactId&gt;
                      &lt;version&gt;3.0.5&lt;/version&gt;
                      &lt;scope&gt;compile&lt;/scope&gt;
                  &lt;/dependency&gt;
                  &lt;dependency&gt;
                      &lt;groupId&gt;org.apache.tiles&lt;/groupId&gt;
                      &lt;artifactId&gt;tiles-servlet&lt;/artifactId&gt;
                      &lt;version&gt;3.0.5&lt;/version&gt;
                      &lt;scope&gt;compile&lt;/scope&gt;
                  &lt;/dependency&gt;
                  &lt;dependency&gt;
                      &lt;groupId&gt;org.apache.tiles&lt;/groupId&gt;
                      &lt;artifactId&gt;tiles-template&lt;/artifactId&gt;
                      &lt;version&gt;3.0.5&lt;/version&gt;
                      &lt;scope&gt;compile&lt;/scope&gt;
                  &lt;/dependency&gt;
                  &lt;dependency&gt;
                      &lt;groupId&gt;org.apache.tiles&lt;/groupId&gt;
                      &lt;artifactId&gt;tiles-el&lt;/artifactId&gt;
                      &lt;version&gt;3.0.5&lt;/version&gt;
                      &lt;scope&gt;compile&lt;/scope&gt;
                  &lt;/dependency&gt;
  
                  &lt;!-- Spring 4 dependencies --&gt;
                  &lt;dependency&gt;
                      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
                      &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
                      &lt;version&gt;${spring.version}&lt;/version&gt;
                  &lt;/dependency&gt;
  
                  &lt;dependency&gt;
                      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
                      &lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt;
                      &lt;version&gt;${spring.version}&lt;/version&gt;
                  &lt;/dependency&gt;
  
  
                  &lt;dependency&gt;
                      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
                      &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
                      &lt;version&gt;${spring.version}&lt;/version&gt;
                  &lt;/dependency&gt;
  
                  &lt;dependency&gt;
                      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
                      &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
                      &lt;version&gt;${spring.version}&lt;/version&gt;
                  &lt;/dependency&gt;
              &lt;/dependencies&gt;
              &lt;build&gt;
                  &lt;finalName&gt;TilesDemo&lt;/finalName&gt;
              &lt;/build&gt;
  
          &lt;/project&gt;
  </code></pre>
  
  <p>Keep The header,footer,menu as per the requirements. </p>
  
  <p>This is <strong>defaultLayout.jsp</strong> page</p>
  
  <pre><code>        &lt;%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%&gt;
          &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%&gt;
          &lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
              pageEncoding="ISO-8859-1"%&gt;
          &lt;html&gt;
          &lt;head&gt;
          &lt;title&gt;&lt;tiles:insertAttribute name="title" ignore="true"&gt;&lt;/tiles:insertAttribute&gt;&lt;/title&gt;
          &lt;/head&gt;
          &lt;body style="background-color: #FFF"&gt;
              &lt;div class="page"&gt;
                  &lt;tiles:insertAttribute name="header" /&gt;
                  &lt;div class="content"&gt;
                      &lt;div id="body"&gt;
                          &lt;tiles:insertAttribute name="body" /&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;tiles:insertAttribute name="footer" /&gt;
              &lt;/div&gt;
          &lt;/body&gt;
          &lt;/html&gt;
  </code></pre>
  
  <p><strong>footer.jsp</strong></p>
  
  <pre><code>    &lt;hr /&gt;
      &lt;div class="span-1 prepend-3"&gt;&amp;nbsp;&lt;/div&gt;
      &lt;div class="span-16 last"&gt;
          &lt;p&gt;
              &lt;b&gt; Technology&lt;/b&gt; 
              ( All rights Reserved)
          &lt;/p&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p><strong>header.jsp</strong></p>
  
  <pre><code>    &lt;div class="span-24"&gt;
          &lt;img src="resources/images/images.png" 
              width="950" style="padding-top:10px;" /&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p><strong>menu.jsp</strong>
  Left side menu bar options</p>
  
  <pre><code>    &lt;%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%&gt;
      &lt;ul style="list-style:none;line-height:28px;"&gt;
  
          &lt;li&gt;&lt;spring:url value="/index" var="homeUrl" htmlEscape="true" /&gt;
              &lt;a href="/TilesDemo/welcome"&gt;Home&lt;/a&gt;
          &lt;/li&gt;
  
          &lt;li&gt;&lt;spring:url value="/viewPerson" var="personListUrl" htmlEscape="true" /&gt;
              &lt;a href="/TilesDemo/viewPerson"&gt;Person List&lt;/a&gt;
          &lt;/li&gt;
  
      &lt;/ul&gt;
  </code></pre>
  
  <p><strong>welcome.jsp</strong><br>
  This is the body Page on which header, footer and menu will be applied.</p>
  
  <pre><code>    &lt;div style="margin:10px;"&gt;
          &lt;h3&gt;SpringMVC - Tiles3 Integration tutorial&lt;/h3&gt;
          &lt;p&gt;By:- XYZ&lt;/p&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>The problem is the that tiles configuration is not getting applied everytime.</p>