Definition files can take the form of an XML document, which is more powerful than JSP definitions. In the previous applications (app12a and app12b) each JSP references a layout or a definition page. Associated with each JSP page is a -body.jsp page that defines the body content of the JSP. For example, the displayAddProductForm.jsp pages in both app12a and app12b are associated with a displayAddProductForm-body.jsp page. With XML type definition, you will not need -body.jsp pages. Instead, the layout will be contained in the definition page, and each JSP page will contain the body content. This is to say that the content of displayAddProductForm-body.jsp will move to displayAddProductForm.jsp and you can say goodbye to displayAddProductForm-body.jsp. Therefore, you can reduce the number of JSPs in your application by almost 50%.
The app12c application modifies the app12b application and shows you how to write and use an XML definition file. But, first note that to use XML definition, you must specify the Tiles plug-in in your Struts configuration file.
<plug-in className="org.apache.struts.tiles.TilesPlugin">
<set-property property="definitions-config"
value="/WEB-INF/tiles-defs.xml"/>
</plug-in>
This plug-in element tells Struts that the name of the Tiles definition file is tiles-defs.xml and it can be found under the WEB-INF directory.
To use the definition, you must change your struts-config.xml. In your action, you do not forward control to a JSP, but to a variable in the Tiles XML definition file. Listing 12.6 presents the action-mappings element in the struts-config.xml file.
Listing 12.6: The action-mappings element in the struts-config.xml file.
<action-mappings>
<action path="/displayAddProductForm"
forward="display.add.product.form"/>
<action path="/saveProduct"
name="productForm"
forward="display.message"/>
</action-mappings>
In the action tags above, the first action forwards control to display.add.product.form and the second to display.message. display.add.product.form and display.message are variables in the XML definition file tiles-def.xml in Listing 12.7.
Listing 12.7: The tiles-def.xml file.
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN"
"http://struts.apache.org/dtds/tiles-config_1_1.dtd">
<tiles-definitions>
<definition name="display.add.product.form"
path="/jsp/myLayout.jsp">
<put name="pageTitle" value="Add Product" />
<put name="header" value="/jsp/header.jsp" />
<put name="body" value="/jsp/displayAddProductForm.jsp" />
<put name="footer" value="/jsp/footer.jsp" />
</definition>
<definition name="display.message" path="/jsp/myLayout.jsp">
<put name="pageTitle" value="Confirmation" />
<put name="header" value="/jsp/header.jsp" />
<put name="body" value="/jsp/displayMessage.jsp" />
<put name="footer" value="/jsp/footer.jsp" />
</definition>
</tiles-definitions>
XML definition can do what JSP definition can. However, since XML definition is written in an XML file, you cannot use the tags in the Tiles Tag Library. Instead, an XML definition XML document is governed by the document type definition (DTD) named tiles-config_1_1.dtd. You will learn more about this DTD in the section “Writing XML Definition” later in this chapter.
The good thing when using XML definition is, as previously mentioned, there is no more –body.jsp pages. Instead, your displayAddProductForm.jsp contains the body of the JSP. Listing 12.8 presents the displayAddProductForm.jsp page.
Listing 12.8: The displayAddProductForm.jsp in app12c
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%> <html:form action="/saveProduct"> <table> <tr> <td>Product Name:</td> <td><html:text property="productName"/></td> <td><html:submit>Add</html:submit></td> </tr> </table> </html:form>
In app12a and app12b the content of the page in Listing 12.8 was the content of the displayAddProductForm-body.jsp. Using XML definition has the advantage of having fewer files.
You can run the application by invoking this URL.
http://localhost:8080/app12c/displayAddProductForm.do
Note
When using XML Tiles definition, the value of the forward and input attributes of an action element can be a variable name in the definition file. So is the value of the path attribute of the forward element that you reference from an action object.
The section “Writing Tiles XML Definition” explains how to write XML definition files.