Over a million developers have joined DZone.

How to Reuse Tiles Definitions Using Wildcard

DZone's Guide to

How to Reuse Tiles Definitions Using Wildcard

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

If you are developing a project in Java/J2EE and using Tiles then you are aware of Tiles definition XML file. Being a 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 name="user/manager" extends="DefaultTemplate">
  <put-attribute name="body"   value="/views/manager.jsp"/>

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

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"/>

Spring configuration xml file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	<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" 
	<bean id="tilesConfigurer" 
		<property name="definitions">


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


Download Source (size: 60 Kb)

Download WAR (size: 4.61 Mb)

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.


Published at DZone with permission of Tousif Khan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}