The JSPs in the app12a application must pass four values: pageTitle, header, body, and footer, by using the put tag. As you can see, the values of header and footer are the same for all JSPs. How do you avoid writing the same put tags in the JSPs in the whole application? By writing a definition page.
A definition page is a layer between a layout page and all JSPs using the layout. The benefit of using a definition page is that you can define values that otherwise have to be defined in referencing JSPs.
By analogy, a layout page is like a Java interface and a definition page is a base class that provides methods’ implementations of the interface. Any Java class that needs to implement the interface can extend the base class instead, so that the class does not need to implement a method unless it needs to override it. By the same token, a JSP can reference a definition page instead of a layout JSP. The diagram in Figure 12.2 provides a comparison between Java inheritance and Tiles’ layout and definition pages.
Figure 12.2: Comparing Java inheritance and Tiles’ layout and definition
There are two types of definition pages: JSP and XML. This section explains how you can write the first type and the next section will discuss the second type.
The app12b application modifies app12a to use a definition file in conjunction with the same layout JSP presented in Listing 12.1. The definition page is named myDefinition.jsp page and is given in Listing 12.4.
Listing 12.4: The myDefinition.jsp page
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%> <tiles:definition id="default" page="/jsp/myLayout.jsp" scope="request"> <tiles:put name="header" value="/jsp/header.jsp" /> <tiles:put name="footer" value="/jsp/footer.jsp" /> </tiles:definition>
The definition file in Listing 12.4 uses the definition tag to reference a layout page. The tag nests two put tags to specify the values of the logical names header and footer that need to be passed to the layout page.
A JSP that needs to use the myLayout.jsp layout page needs to reference the myDefinition.jsp page and define the logical names and variables that have not been defined in the definition file or that need to be overridden . Listing 12.5 presents the displayAddProductForm.jsp page in app12b, which is slightly shorter than the same file in app12a.
Listing 12.5: The displayAddProductForm.jsp page in app12b.
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%> <%@include file="/jsp/myDefinition.jsp" %> <tiles:insert beanName="default" beanScope="request"> <tiles:put name="pageTitle" value="Add Product"/> <tiles:put name="body" value="/jsp/displayAddProductForm-body.jsp"/> </tiles:insert>
Now, you only need to use two put tags (pageTitle and body) in your JSPs. The other two (header and footer) are already defined in the myDefinition.jsp page.
You can run the app12b application by invoking this URL.
http://localhost:8080/app12b/displayAddProductForm.do
Note
The definition tag is formally explained in the section “The Tiles Tag Library’s Tags” later in this chapter.