Configuring a Tiles ViewResolver

Let's configure Apache Tiles in the Spring MVC application. In order to configure it, we have to configure two beans in the Spring configuration file as follows:

    package com.packt.patterninspring.chapter10.bankapp.web.mvc; 
    ..... 
    @Configuration 
    @ComponentScan(basePackages = {"com.packt.patterninspring.chapter10.bankapp.web.controller"})       
    @EnableWebMvc 
    public class SpringMvcConfig extends WebMvcConfigurerAdapter{ 
    ..... 
    @Bean 
    public TilesConfigurer tilesConfigurer() { 
         TilesConfigurer tiles = new TilesConfigurer(); 
         tiles.setDefinitions(new String[] { 
               "/WEB-INF/layout/tiles.xml" 
         }); 
         tiles.setCheckRefresh(true); 
         return tiles; 
    } 
    
    @Bean 
    public ViewResolver viewResolver() { 
         return new TilesViewResolver(); 
    } 
     ... 
   } 

In the preceding configuration file, we configured two beans, TilesConfigurer and the TilesViewResolver bean. The first bean, TilesConfigurer, has the responsibility to locate and load tile definitions, and, generally, coordinate Tiles. The second bean, TilesViewResolver, is responsible for resolving logical view names to tile definitions. The XML file tiles.xml has the tile definitions in the application. Let's see the following code for the tiles configuration file:

    <tiles-definitions> 
      <definition name="base.definition" template="/WEB-INF/views/mainTemplate.jsp"> 
        <put-attribute name="title" value=""/> 
        <put-attribute name="header" value="/WEB-INF/views/header.jsp"/> 
        <put-attribute name="menu" value="/WEB-INF/views/menu.jsp"/> 
        <put-attribute name="body" value=""/> 
        <put-attribute name="footer" value="/WEB-INF/views/footer.jsp"/> 
      </definition> 
  
      <definition extends="base.definition" name="openAccountForm"> 
        <put-attribute name="title" value="Account Open Form"/> 
        <put-attribute name="body" value="/WEB-INF/views/accountForm.jsp"/> 
      </definition> 
     
      <definition extends="base.definition" name="accountsList"> 
        <put-attribute name="title" value="Employees List"/> 
        <put-attribute name="body" value="/WEB-INF/views/accounts.jsp"/> 
      </definition> 
      ... 
      ... 
    </tiles-definitions> 

In the preceding code, the <tiles-definitions> element has multiple <definition> elements. Each <definition> element defines a tile, and each tile references a JSP template. Some <definition> elements extend the base tile definition, because the base tile definition has the common layout for all the views in the web application.

Let's see the base definition template, that is, mainTemplate.jsp:

    <%@ taglib uri="http://www.springframework.org/tags" prefix="s" %> 
    <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="t" %> 
    <%@ page session="false" %> 
    <html>   
      <head>   
        <title> 
          <tiles:insertAttribute name="title" ignore="true"/> 
        </title> 
      </head> 
      <body> 
         <table border="1″ cellpadding="2″ cellspacing="2″ align="left"> 
               <tr> 
                     <td colspan="2″ align="center"> 
                           <tiles:insertAttribute name="header"/> 
                     </td> 
               </tr> 
               <tr> 
                     <td> 
                           <tiles:insertAttribute name="menu"/> 
                     </td> 
                     <td> 
                           <tiles:insertAttribute name="body"/> 
                     </td> 
               </tr> 
               <tr> 
                     <td colspan="2″  align="center"> 
                           <tiles:insertAttribute name="footer"/> 
                     </td> 
               </tr> 
         </table> 
       </body>   
    </html> 

In this preceding JSP file, I have used the <tiles:insertAttribute> JSP tag from the tiles tag library to insert other templates.

Let's now see some best practices used to design and develop a web application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset