Tiles component combination
Tiles components are reusable components. You can build a simple
The tiles component is assembled into a complex tiles component. For example, you can split the left part of the tiles component named "index-definition" into independent tiles groups.
File, named "sidebar-definition", as shown in 16-6.
Figure 16-6 split the left part of the tiles component named "index-definition" into independent tiles Components
The following describes how to use the combined tiles component in the tilestaglibs application.
(1) Redefine the three tiles components "sidebar-definition", "index-definition", and "product-definition" in the tiles-def.xml file. The syntax for one tiles component to contain another tiles component is:
<definition name="index-definition" path="/layout.jsp">
<put name="sidebar" value="sidebar-definition" type="definition"/>
……
</definition>
The Value Attribute of the put sub-element specifies the name of the contained tiles component. The type attribute is set to "Definition", indicating that the value attribute specifies the tiles component rather than the JSP file. The routine 16-18 is the code for the tiles-def.xml file.
Routine 16-18 tiles-def.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration//EN"
"http://jakarta.apache.org/struts/dtds/tiles-config.dtd">
<tiles-definitions>
<definition name="sidebar-definition" path="/sidebar-layout.jsp">
<put name="top" value="flags.jsp"/>
<put name="bottom" value="sidebar-links.jsp"/>
</definition>
<definition name="index-definition" path="/layout.jsp">
<put name="sidebar" value="sidebar-definition" type="definition"/>
<put name="header" value="header.jsp"/>
<put name="content" value="indexContent.jsp"/>
<put name="footer" value="footer.jsp"/>
</definition>
<definition name="product-definition" path="/layout.jsp">
<put name="sidebar" value="sidebar-definition" type="definition"/>
<put name="header" value="header.jsp"/>
<put name="content" value="productContent.jsp"/>
<put name="footer" value="footer.jsp"/>
</definition>
</tiles-definitions>
(2) create a JSP file for the tiles component named "sidebar-definition.
Name
The template file for the tiles component of "sidebar-definition" is a sidebar-layout.jsp and is inserted into two JSP files in this template
Don't be: Flags. jsp and sidebar-links.jsp. Examples 16-19, 16-20, and 16-21 are the source code of these JSP files.
Routine 16-19 sidebar-layout.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%>
<table >
<tr>
<%-- Sidebar top component --%>
<tiles:insert attribute="top"/>
</tr>
<tr>
<%-- Sidebar bottom component --%>
<tiles:insert attribute="bottom"/>
</tr>
</table>
Routine 16-20 sidebar-links.jsp
<%-- Sidebar bottom component --%>
<td>
<table>
<tr>
<td>
<font size="5">Links</font><p>
<a href="index.jsp">Home</a><br>
<a href="product.jsp">Products</a><br>
<a href="">Hot Link1</a><br>
<a href="">Hot Link2</a><br>
<a href="">Hot Link3</a><br>
</td>
</tr>
</table>
</td>
Example 16-21 flags. jsp
<%-- Sidebar top component --%>
<td width="150" height="65" valign="top" align="left">
<a href=""></a>
<a href=""></a>
</td>
Tiles component Extension
In
In the tiles-def.xml file of section 16.5.3, "index-definition" and "product-definition" are two tiles groups
Duplicate code still exists in the definition of the component. You can use the scalability of the tiles component to further eliminate redundant code. Solution: first define a parent class that contains the common content of the two tiles components.
Tiles component, named "base-definition", and then "index-definition" and "product-definition"
The two tiles components inherit the parent class component. Figure 16-7 shows the improved tiles component relationship.
Figure 16-7 relationship between improved tiles Components
The syntax for one tiles component to inherit from another tiles component is as follows. The extends attribute of the definition element specifies the extended parent tiles component:
<definition name="index-definition" extends="base-definition">
The routine 16-22 is the code for the improved tiles-def.xml.
Routine 16-22 tiles-def.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd">
<tiles-definitions>
<definition name="sidebar-definition" path="/sidebar-layout.jsp">
<put name="top" value="flags.jsp"/>
<put name="bottom" value="sidebar-links.jsp"/>
</definition>
<definition name="base-definition" path="/layout.jsp">
<put name="sidebar" value="sidebar-definition" type="definition"/>
<put name="header" value="header.jsp"/>
<put name="content" value=""/>
<put name="footer" value="footer.jsp"/>
</definition>
<definition name="index-definition" extends="base-definition">
<put name="content" value="indexContent.jsp"/>
</definition>
<definition name="product-definition" extends="base-definition">
<put name="content" value="productContent.jsp"/>
</definition>
</tiles-definitions>