The tiles framework allows you to configure the tiles component in a specialized XML file. For example, the following code defines a tiles component named "index-definition", which describes the entire index. jsp webpage:
<tiles-definitions> <definition name="index-definition" path="/layout.jsp"> <put name="sidebar" value="sidebar.jsp"/> <put name="header" value="header.jsp"/> <put name="content" value="indexContent.jsp"/> <put name="footer" value="footer.jsp"/> </definition></tiles-definitions>
|
The name attribute of the definition element specifies the name of the tiles component, and the path attribute specifies the template used by the tiles component. The put sub-element of the definition element is used to insert specific webpage content to the template.
Tip:The source program of the tilestaglibs application described in this section is located in the sourcecode/tilestaglibs/Version5/tilestaglibs directory of the auxiliary CD. To publish this application on Tomcat, copy the entire tilestaglibs subdirectory under the Version5 directory to the catalina_home/webapps directory.
The following describes how to use the tiles component in the tilestaglibs application.
(1) The files required to install the tiles tag library, as shown in 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) configure the tiles component in a specialized XML file, in this example name this configuration file as a tiles-defs.xml, which is located under the WEB-INF directory. Routine 16-15 is the code for the tiles-defs.xml file.
Routines 16-15 tiles-defs.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="index-definition" path="/layout.jsp"> <put name="sidebar" value="sidebar.jsp"/> <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.jsp"/> <put name="header" value="header.jsp"/> <put name="content" value="productContent.jsp"/> <put name="footer" value="footer.jsp"/> </definition></tiles-definitions>
|
The above Code defines two tiles components, which represent the complete index. jsp and product. JSP pages respectively.
(4) configure the tilesplugin plug-in the strut configuration file. The Code is as follows:
<plug-in className="org.apache.struts.tiles.TilesPlugin" > <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" /> <set-property property="definitions-parser-validate" value="true" /></plug-in>
|
The tilesplugin plug-in is used to load the configuration file of the tiles component. The plug-in element contains several set-property sub-elements for passing additional parameters to the tilesplugin plug-in:
· Definitions-config parameter: Specifies the configuration file of the tiles component. If multiple configuration files exist, they are separated by commas.
· Definitions-parser-validate parameter: Specifies whether the XML Parser verifies the tiles configuration file. Optional values include true and false. The default value is true.
(5) Configure actionservlet in the web. xml file
To ensure that the tilesplugin plug-in is loaded when the web application starts, the actionservlet controller should be added. The actionservlet controller can load all the plug-ins during initialization. The following code configures actionservlet in the web. xml file:
<servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <load-on-startup>3</load-on-startup></servlet><servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern></servlet-mapping>
|
(6) Insert the tiles component in index. jsp and product. jsp. For details, see routines 16-16 and routines 16-17:
Example 16-16 index. jsp
<% @ Page contenttype = "text/html; charset = UTF-8" %> <% @ taglib uri = "/WEB-INF/struts-tiles.tld" prefix = "tiles" %> <tiles: insert definition = "index-definition"/> & nbsp; routine 16-17 product. JSP <ccid_nobr> <Table width = "400" border = "1" cellspacing = "0" cellpadding = "2" bordercolorlight = "black" bordercolordark = "# ffffff" align =" center "> <tr> <TD bgcolor =" e6e6e6 "class =" code "style =" font-size: 9pt "> <PRE> <ccid_code> <% @ page contenttype =" text/html; charset = UTF-8 "%> <% @ taglib uri ="/WEB-INF/struts-tiles.tld "prefix =" tiles "%> <tiles: insert definition =" product-definition "/>
|
Use struts action to call the tiles component
If the tiles component represents a complete web page, you can directly call the tiles component through struts action. For example, if you want to use struts action to call the tiles component named "index-definition" defined in section 16.5.1, you can configure the following action ing in the struts configuration file:
<action-mappings><action path="/index" type="org.apache.struts.actions.ForwardAction" parameter="index-definition"> </action></action-mappings>
|
Next, access http: // localhost: 8080/tilestaglibs/index through the browser. do: the request is first forwarded to forwardaction, and forwardaction then forwards the request to the tiles component named "index-definition". Finally, on the browser side, the user will see and index. the same JSP page.
The struts action is used to call the tiles component. The Struts framework is responsible for process control. In addition, the number of JSP files can be reduced. For example, if you use struts action to call the tiles component named "index-definition", you do not have to create the index. jsp file. (T111)
This article is selected from the Apsara stack library "master struts: MVC-based Java Web design and development".