Saturday, April 13, 2013

Spring Tiles Integration

Suppose if your web page has a standard header, footer and only center content will be changed, it is very difficult to hardcode in each and every webpage and if later if any changes is needed then all pages needs to be updated with the relevant details. Here comes Tiles which helps you to templatize the common items and include it in each and every page.

Lets see how we are going to integrate the Titles in our Spring MVC. Our application layout will have the standard header and footer across all web pages, where only the center content will be changed.

Make sure that you have the following jars files included in your lib

commons.beanutils.jar
commons-digester-2.1.jar
commons-logging-1.1.jar
tiles-api-2.0.4.jar
tiles-core-2.0.4.jar
tiles-jsp-2.0.4.jar

Configuring titles framework in Spring MVC

 
    
    


   
       
          /WEB-INF/tiles-def.xml
        
   
	

Using the definitions attribute, we need to specify the tiles definition file. The tiles-def.xml definitions are below, the file should be located under WEB-INF directory in your application. In the tiles-def.xml we need to define our base layout structure. The base layout we are building contatins the attributes title, header, body and footer. We need to create the baseLayout.jsp and place it under /WEB-INF/titles, the template baseLayout.jsp, will contain the different segments of a web page, header, body and footer.


   
      
          
      
      		
   
   
      
      			
   	
   
      	
      	
   	


BaseLayout.jsp - View Template
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
..
..
<tiles:insertAttribute name="title" ignore="true"/>






..

To display the views, we use the ResourceBundleViewResolver. By default the view.properties will be used to store
the key value pairs

hello.(class)=org.springframework.web.servlet.view.tiles2.TilesView
hello.url=base

dealerlist.(class)=org.springframework.web.servlet.view.tiles2.TilesView
dealerlist.url=dealer

Controller File
ModelMap modelMap = new ModelMap();
modelMap.addAttribute("dealerList" , dealerList);		
return new ModelAndView("dealerlist", modelMap);

The base, delear in view.properties refers to the definition name in the tiles-def.xml file. The hello and dealerlist refers to the modelview name sent by your controller.

That's all you have integrated the Tiles with Spring MVC

Happy Programming...!!!

No comments:

Post a Comment