页面规划Templates
Template.jsp:
<%@ page contentType="text/html; charset=GB2312" %>
<%@ taglib uri='/WEB-INF/struts-template.tld' prefix='tmp' %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<html><head>
<link rel="stylesheet" href="css/templates.css"
charset="GB2312" type="text/css">
</head>
<table>
<tr valign='top'>
<td><tmp:get name='sidebar'/></td>
<td><table>
<tr><td><tmp:get name='header'/></td></tr>
<tr><td><tmp:get name='content'/></td></tr>
<tr><td><tmp:get name='footer'/></td></tr>
</table>
</td>
</tr>
</table>
<body>
</body>
</html>
Index.jsp:
<%@ page contentType="text/html; charset=GB2312" %>
<%@ taglib uri='/WEB-INF/struts-template.tld' prefix='template' %>
<template:insert template='/Template.jsp'>
<template:put name='sidebar' content='/sidebar.jsp' direct='true'/>
<template:put name='header' content='/header.jsp' />
<template:put name='content' content='/ content.jsp'/>
<template:put name='footer' content='/footer.jsp' />
</template:insert>
Installing and Configuring Tiles
1.Installing the Required JARs and Misc Files
2.Adding the Tiles Tag Library
Adding the Tiles Tag Library
Like any other JSP tag library, you must include it within the web application deployment descriptor to use it. Add the following taglib element to the web.xml file:
<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>
</jsp-config>
Configuring Tiles to Work with Struts
<plug-in className="org.apache.struts.tiles.TilesPlugin" >
<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
<set-property property="definitions-debug" value="2" />
<set-property property="definitions-parser-details" value="2" />
<set-property property="definitions-parser-validate" value="true" />
</plug-in>
What is a Tile?
A Tile is an area or region within a web page. The region can be the entire web page, or the page can be broken up into several regions. Figure 14-1 illustrates an example from the Storefront application.
Template using tiles
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<html:html> <head>
<body topmargin="0" leftmargin="0" bgcolor="#FFFFFF">
<!-- Header Page Information -->
<tiles:insert attribute="header" />
<!-- Menu Bar -->
<tiles:insert attribute="menubar"/>
<!-- Main Body Information -->
<tiles:insert attribute="body-content"/>
<!-- Copyright Information -->
<tiles:insert attribute="copyright"/>
</body>
</html:html>
using a Template
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<tiles:insert page="/Template.jsp " flush="true">
<tiles:put name="header" value="/header.jsp" />
<tiles:put name="sidebar" value="/sidebar.jsp" />
<tiles:put name="content" value="/content.jsp" />
<tiles:put name="footer" value="/footer.jsp" />
</tiles:insert>
Using Definitions
Declaring Definitions in a JSP Page
Declaring Definitions in a Configuration File
Declaring Definitions in a JSP Page
<%@ taglib uri="/WEB-INF/tiles.tld" prefix="tiles" %>
<tiles:definition
id="storefront.default"
page="/Template.jsp"
scope="request">
<tiles:put name="header" value="/header.jsp" />
<tiles:put name="sidebar" value="/sidebar.jsp" />
<tiles:put name="footer" value="/footer.jsp" />
</tiles:definition>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<%@include file="/Template.jsp" %>
<tiles:insert beanName="storefront.default" beanScope="request">
<tiles:put name="content" value="/content.jsp"/>
</tiles:insert>
Declaring Definitions in a Configuration File
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration//EN"
"http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd">
<tiles-definitions>
<definition name="storefront.default" path="/Template.jsp">
<put name="header" value="/header.jsp" />
<put name="sidebar" value="/sidebar.jsp" />
<put name="footer" value="/footer.jsp" />
</definition>
<definition name="storefront.custom" extends="storefront.default">
<put name="content" value="/content.jsp" />
</definition>
</tiles-definitions>
调用:
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<tiles:insert definition="storefront.custom" flush="true" />
Using Definitions as Forwards in Struts
<global-forwards>
<forward name="Super_Success" path="storefront.superuser.main" />
</global-forwards>