Nesting Tiles

 < Day Day Up > 



One Tile layout can insert another Tile layout, and so on. In fact, you can create Tile layouts that are so small they are not really templates per se. Instead, they become small visual components more similar to custom tags than page templates.

Remember the logic we implemented for displaying a link. We check to see if the link begins with / to determine whether the link is relative and then display it correctly. What if you want to use that same routine in several places in your application? You can create a visual component to do this.

Creating a Visual Component

A visual component is just another Tile layout. Whether a Tile layout is a visual component or a template is really just a point of view. The Tile layout in Listing 13.8 defines a visual component for displaying a link.

Listing 13.8: linkLayout.jsp.

start example
 <%@ 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" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>    <tiles:useAttribute             name="item"           classname="org.apache.struts.tiles.beans.MenuItem"                     />    <bean:define  name="item" property="link"                 type="java.lang.String"/>    <logic:match name="link" location="start" value="/">        <html:link page="<%=link%>" >            <bean:write name="item" property="value"/>              </html:link>    </logic:match>    <logic:notMatch name="link" location="start" value="/" >        <html:link href="<%=link%>">            <bean:write name="item" property="value"/>              </html:link>    </logic:notMatch> 
end example

The nice thing about using this approach rather than JSP custom tags is that you can use other custom tags. Also, this is document-centric JSP versus a Java class (like a custom tag).

start sidebar

Many of the advantages of using Tile layouts in this manner can now be achieved with JSP tag files, which are available in JSP 2.0 and later. If you are using an older version of JSP that does not support tag files, you can start using this technique now.

end sidebar

Using the Visual Component

Once you have defined the visual component, you need to create a definition for it:

   <definition name="linkLayoutDef" path="/linkLayout.jsp">   </definition> 

At this point, you can use your visual component on any page by using the tiles:insert tag. You even use this visual component inside another Tile. The following code uses this visual component inside the Tile layout we defined earlier:

 ...   <td width="50%">        <ul>        <logic:iterate  name="items"               type="org.apache.struts.tiles.beans.MenuItem" >           <li>                     <tiles:insert definition="linkLayoutDef">                    <tiles:put name="item"                                beanName="item"                                beanScope="page"/>                     </tiles:insert>           </li>              </logic:iterate>        </ul>        </td> ... 

This code iterates over the list of items, then calls tiles:insert and passes the current item to the visual component (linkLayoutDef) to display. The visual component knows how to display a domain object (a menu item). Any time you catch yourself repeating the same lines of JSP code over and over again, consider writing a visual component using a Tile layout.

Using a Tile as a Parameter to Another Tile

The previous example explicitly called the visual component that we defined. What if the Tile layout you use varies based on several factors (whether the user is logged in, whether the user is in a certain role, which part of the site you are looking at)? Wouldn't it be nice to be able to pass the Tile as a parameter? You can do just that by using the put element, as shown in this code (tiles-def.xml):

   <definition name="link.layout.def" path="/linkLayout.jsp">   </definition>   <definition name="siteLayoutDef7" path="/siteLayout5.jsp"                                          extends="siteLayoutDef4">     <put name="title" value="WROX Quote System 9" />     <putList name="items" >     </putList>     <put name="linkDisplay" value="link.layout.def"/> </definition> 

Notice that siteLayoutDef7 has an attribute called linkDisplay. The value of linkDisplay is equal to link.layout.def. Now inside the Tile layout (siteLayout5.jsp) you can specify the attribute linkDisplay instead of calling a particular Tile layout definition:

         <ul>         <logic:iterate  name="items"                type="org.apache.struts.tiles.beans.MenuItem">            <li>             <tiles:insert attribute="linkDisplay">                    <tiles:put name="item"                               beanName="item"                               beanScope="page"/>             </tiles:insert>            </li>               </logic:iterate>         </ul> 

In this example, the site layout does not know which visual component it is using. You can programmatically switch how pieces of the layout are displayed by switching which visual component the site layout uses.



 < Day Day Up > 



Professional Jakarta Struts
Professional Jakarta Struts (Programmer to Programmer)
ISBN: 0764544373
EAN: 2147483647
Year: 2003
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net