Home

This Chapter
-Chapter 12: The Tiles Framework
-The Problem with JSP Includes
-A Taste of Tiles
-Using JSP Definition Files
-Using XML Definition
-Inheritance of XML Definition
-The Tiles Tag Library’s Tags
-Writing Tiles XML Definition
-Summary

Table of Contents
-Introduction
-Chapter 1: Model 2 and Struts
-Chapter 2: Input Validation with Action Forms
-Chapter 3: The HTML Tag Library
-Chapter 4: Input Validation and Data Conversion
-Chapter 5: The Validator Plugin
-Chapter 6: The Expression Language
-Chapter 7: JSTL
-Chapter 8: The Bean Tag Library
-Chapter 9: The Logic Tag Library
-Chapter 10: Struts-EL, Nested, selectLabel
-Chapter 11: Message Handling and Internationalization
-Chapter 12: The Tiles Framework
-Chapter 13: Securing Struts Applications
-Chapter 14: The Config Object
-Chapter 15: The Persistence Layer
-Chapter 16: Object Caching
-Chapter 17: File Upload and File Download
-Chapter 18: Paging and Sorting
-Chapter 19: Preventing Double Submits
-Chapter 20: Early HttpSession Invalidation
-Chapter 21: Decorating Request Objects
-Chapter 22: How Struts Works

Previous
Next

 

Using JSP Definition Files

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.

Previous
Next