Tile
Definitions
- Create a Tile Definition
A layout may include many regions and we may
have to repeat the parameter every time we want
to use a tile layout, it would be better if
we define it at one place instead of in each
page.
We
invoke the tile layout using tiles:insert and
pass in parameters using tiles:put. The parameters
are other JSP pages and strings that can be
inserted into regions of the tile layout. We
now need the ability to define default parameters
corresponding to the common regions regions.
The Tiles framework also lets you pass default
arguments to a tile layout using definitions.
Create and use a JSP definition
First import the tiles tag library using the
taglib directive.
<%@ taglib uri="/WEB-INF/struts-logic.tld"
prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld"
prefix="tiles" %>
Second
ensure that the definition is defined only once
using the logic:notPresent tag.
<logic:notPresent name="layoutDef"
scope="application">
Third
define the definition with the tiles:definition
tag passing the JSP page that defines the tile
layout and the scope of the newly defined definition.
<tiles:definition id="layoutDef"
page="/layout.jsp" scope="application">
Fourth
define the default parameters with the tiles:put
tag.
<tiles:put name="title"
type="string" value="DOG"
/>
<tiles:put name="header" value="/header.jsp"
/>
<tiles:put name="footer" value="/footer.jsp"
/>
<tiles:put name="content" type="string">
Some details...
</tiles:put>
</tiles:definition>
</logic:notPresent>
The
tiles:definition tag defines a JavaBean component
of type ComponentDefinition (org.apache.struts.tiles.ComponentDefinition).
ComponentDefinition has getter and setters for
all of the attributes that you can pass it.
The logic:notPresent tag ensures that the ComponentDefinition
is only created once per application by checking
to see if it's already in scope before defining
it.
Using
a tile definition resembles using a tile layout
directly. The only differences: you will specify
the definition instead of the tile layout JSP
page, and you will pass in less parameters with
tiles:put.
|