Quantcast
Channel: TechZoo - Technology Blog » Spring Framework
Viewing all articles
Browse latest Browse all 8

How to reuse Tiles definitions using wildcard

$
0
0


If you are developing project in java/j2ee and using Tiles then you are aware of Tiles definition XML file. Being Templating framework, Tiles used one definition per view to render output. User can extend this definition with a defined template so all the common pages will be rendered by the template but still you need one definition per jsp.

Consider a scenario where you have three user roles (admin, manager, and employee) and for each you have created a separate jsp. With this requirement you might end up with such tiles definitions.

<definition name="user/admin" extends="DefaultTemplate">
  <put-attribute name="body"   value="/views/admin.jsp"/>
</definition>

<definition name="user/manager" extends="DefaultTemplate">
  <put-attribute name="body"   value="/views/manager.jsp"/>
</definition>

<definition name="user/employee" extends="DefaultTemplate">
  <put-attribute name="body"   value="/views/employee.jsp"/>
</definition>

Tiles v2.2 comes with the wildcard character support in definitions name. With the above scenario, we can make these three definitions into one using wildcard.

<definition name="user/*" extends="DefaultTemplate">
  <put-attribute name="body"   value="/views/{1}.jsp"/>
</definition>

Spring configuration xml file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
	http://www.springframework.org/schema/context  
	http://www.springframework.org/schema/context/spring-context-3.2.xsd 
	http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
	
	<mvc:annotation-driven />
	<mvc:default-servlet-handler />
	<mvc:resources mapping="/resources/**" location="/resources/" />

	<!-- Forwards requests to the "/" resource to the "index" view -->
	<mvc:view-controller path="/" view-name="index" />
  
	<mvc:view-controller path="user/admin" view-name="user/admin" />
	<mvc:view-controller path="user/manager" view-name="user/manager" />
	<mvc:view-controller path="user/employee" view-name="user/employee" />
	
	<bean id="viewResolver" 
    class="org.springframework.web.servlet.view.tiles3.TilesViewResolver"/>
    
	<bean id="tilesConfigurer" 
    class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
		<property name="definitions">
			<list>
				<value>/WEB-INF/layouts/layouts.xml</value>
			</list>
		</property>
	</bean>

</beans>

I have used Spring MVC to integrate Tiles and used Spring view controller to avoid creating java Controller.

Output:



Download Source (size: 60 Kb)


Download WAR (size: 4.61 Mb)


Viewing all articles
Browse latest Browse all 8

Latest Images

Trending Articles





Latest Images