Although the tiles: insert tag is used, there are still a lot of repeated code in the index. jsp and product. jsp files. To improve the reusability and maintainability of web pages, the tiles template mechanism can be introduced.
Introduction
The source program of the tilestaglibs application is located in the sourcecode/tilestaglibs/version4/tilestaglibs
Directory. To publish this application on Tomcat, copy the entire tilestaglibs subdirectory in the version4 directory
In the catalina_home/webapps directory.
In general, the tiles template is a JSP page that describes the page layout. The tiles template only defines the web page style, but does not specify the content. When a web application is running, the specific content is inserted into the template page. The same template can be shared by multiple web pages.
By using templates, you can easily maintain the same appearance and layout of all pages of a Web application without the need to hard code each page. In an application, most pages use the same template. Some pages may have different appearances and use other templates. Therefore, an application may have more than one template.
The following describes how to use the tiles template in the tilestaglibs application.
(1) The files required to install the tiles tag library are the same as step 1 in section 16.3.
(2) configure the taglib element in the web. xml file, which is the same as step 2 in section 16.3.
(3) define the template file. For more information, see routines 16-12.
Routine 16-12 layout. jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%>
<title>TilesTaglibs Sample</title>
<body >
<%-- One table lays out all of the content --%>
<table width="100%" height="100%">
<%-- Sidebar section --%>
<tr>
<td width="150" valign="top" align="left" bgcolor="#CCFFCC">
<tiles:insert attribute="sidebar"/>
</td>
<%-- Main content section --%>
<td valign="top" height="100%" width="*">
<table width="100%" height="100%">
<tr>
<%-- Header section --%>
<td height="15%">
<tiles:insert attribute="header"/>
</td>
<tr>
<tr>
<%-- Content section --%>
<td valign="top" height="*">
<tiles:insert attribute="content"/>
</td>
</tr>
<tr>
<%-- Footer section --%>
<td valign="bottom" height="15%">
<tiles:insert attribute="footer"/>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
The layout of the webpage is defined in the template file layout. jsp, but the specific content of each part is not specified. Layout. jsp contains multiple tiles: insert tags. Its Attribute attribute only specifies the logical name of the content to be inserted, but does not specify the truly inserted file.
(4) use the tiles template in index. jsp and product. jsp. For details, see routines 16-13 and routines 16-14.
Example 16-13 index. jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<tiles:insert page="layout.jsp" flush="true">
<tiles:put name="sidebar" value="sidebar.jsp"/>
<tiles:put name="header" value="header.jsp"/>
<tiles:put name="content" value="indexContent.jsp"/>
<tiles:put name="footer" value="footer.jsp"/>
</tiles:insert>
Routine 16-14 product. jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<tiles:insert page="layout.jsp" flush="true">
<tiles:put name="sidebar" value="sidebar.jsp"/>
<tiles:put name="header" value="header.jsp"/>
<tiles:put name="content" value="productContent.jsp"/>
<tiles:put name="footer" value="footer.jsp"/>
</tiles:insert>
In
In index. jsp and product. jsp, tiles: insert tag specifies the inserted template file. Both index. jsp and product. jsp use
The same template file layout. jsp. Tiles: The insert tag contains several tiles: Put sub-tags, which specify the specific content inserted into the template.
Tiles: The name attribute of the put tag matches the attribute of the tiles: insert tag in the template file, and the Value Attribute of the tiles: Put tag.
Specifies the specific JSP file inserted into the template.
The tiles template mechanism greatly improves code reusability and maintainability. The template contains the common
Layout. If the layout changes, you only need to modify the template file without modifying the specific webpage file. However, we can see from the routines 16-13 and 16-14 that although index. jsp and
The length of the product. jsp file is shortened, but the two still have repeated code.