Understanding Lists

 < Day Day Up > 



In many cases, you want to pass more than one parameter. For example, you may want to pass a list of parameters to display links in the navigation region of the Tile layout.

Going back to our employee listing example, suppose you have a Web application that displays a company's divisions, departments, and employees. When you are in the employee listing view, employeeListing.jsp is in the content region of the Tile layout and the department links of the current division appear in the navigation region. When you click on a department link, a new listing of the employees in that department appears. In the department view, the deptListing.jsp displays in the content region of the Tile layout and the list of division links of the company appears in the navigation region. When you click on a division link, a new listing of departments appears. Thus, each page (employeeListing.jsp and deptListing.jsp) passes in a new list of links.

Using putList in XML

Tiles allow users to pass in links using the putList sub-element, which can be used in XML and JSP definitions or in calls to a definition or Tile layout from a JSP.

Suppose you want to have a standard set of navigational links for your site layout. You can specify these links with the putList sub-element in your Tiles configuration file (tiles-def.xml), shown in Listing 13.5.

Listing 13.5: tiles-def.xml.

start example
   <definition name="siteLayoutDef3" path="/siteLayout3.jsp">     <put name="title" value="WROX Stock Quote System" />     <put name="header" value="/header2.jsp" />     <put name="footer" value="/footer.jsp" />     <put name="content" type="string">        Content goes here     </put>     <putList name="items" >       <item value="Home"             link="/index.html"  />       <item value="WROX"             link="http://www.wrox.com"  />       <item value="Trivera Technologies"             link="http://www.triveratech.com/"  />       <item value="Virtuas"             link="http://www.virtuas.com/"  />       <item value="Rick Hightower"             link="http://www.rickhightower.com"  />       <item value="Rick's Blog"             link="http://rickhightower.blogspot.com/"  />     </putList>   </definition> 
end example

The putList element lets you specify a list of items associated with links. In Listing 13.5, putList defines six links. The list (java.util.List) put into Tile scope consists of items specified with the putList element's name attribute.

The item element defines a link by putting an instance of org.apache.struts.tiles.beans.MenuItem in the list. The value attribute corresponds to the label on the link, and the link element refers to the link's URL.

start sidebar

The item element also has elements for specifying the tool tip and the icon for the link. You can learn more about the item element and putList by looking at tiles-config_1_1.dtd, which can be found in the Struts source.

end sidebar

Using the List in the Tile Layout

To use the list of links, you need to modify your Tile layout (siteLayout3.jsp) as shown in Listing 13.6. The changes appear in bold.

Listing 13.6: siteLayout3.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:importAttribute /> <html>   <head>     <logic:present name="title">         <title>          <tiles:getAsString name="title" ignore="true"/>          </title>      </logic:present>    </head>    <body>      <table width="500" border="0" cellspacing="0" cellpadding="0">        <tr bgcolor="#36566E">          <td height="68" width="70%">            <div align="left">     <img src="/books/2/574/1/html/2/images/hp_logo_wrox.gif" width="220" height="74">            </div>          </td>        </tr>        <tr>          <td height="68" width="2000">              <tiles:insert attribute="header" ignore="true">                 <tiles:put name="title"                            beanName="title" beanScope="tile"/>                 <tiles:put name="user"                            beanName="user" beanScope="session"/>              </tiles:insert>           </td>        </tr>        <table>        <tr>          <td width="50%">          <ul>          <logic:iterate  name="items"                  type="org.apache.struts.tiles.beans.MenuItem" >             <li>             <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>             </li>                </logic:iterate>          </ul>          </td>          <td width="50%">              <div align="center">              <tiles:insert attribute="content"/>              </div>          </td>        </tr>        </table>        <tr>          <td>              <tiles:insert attribute="footer" ignore="true"/>          </td>        </tr>      </table>   </body> </html> 
end example

The tiles:importAttribute tag

The tiles:importAttribute tag imports all of the attributes in Tile scope into page scope. It is similar to the tiles:useAttribute tag—but much more like a shotgun than a scalpel. It is lazy, dirty and cheap; I use it all the time. This effectively copies the items list from Tile scope into page scope.

start sidebar

By default, tiles:importAttribute copies all of the attributes into page scope. You can copy the attributes into other scopes as well by using the scope attribute.

end sidebar

Once the items list is in page scope, you can access it with the standard Struts tags:

        <logic:iterate  name="items"           type="org.apache.struts.tiles.beans.MenuItem" >         ...       </logic:iterate> 

Notice the logic this code implements with the logic tag to display the link. First, the code checks to see if the link begins with a slash (/) to determine if the link is relative. If the link is relative, the code uses the page attribute of the html:link tag; otherwise, it uses the href attribute of the html:link tag (if the link refers to an absolute URL):

          <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> 

You may want to use this bit of display logic to display menu items in more than one location—in others words, you may want to reuse it outside the scope of this page. In the section "Nesting Tiles," we show you how to do this by nesting one Tile layout into another.

Using putList in JSP

In addition to adding items to the list in the Tile definition, you can add items to the list in the JSP using the tiles:putList element and its tiles:add sub-element, as shown in Listing 13.7 (index6.jsp).

Listing 13.7: index6.jsp.

start example
 <%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %> <%@ page import="org.apache.struts.tiles.beans.SimpleMenuItem" %> <tiles:insert definition="siteLayoutDef4">     <tiles:put name="title" type="string"                value="Get WROX Stock Quote6" />     <tiles:put name="content" value="indexContent5.jsp"/>     <tiles:putList name="items" >         <jsp:useBean  />         <jsp:setProperty name="item1" property="link"                          value="/index.html"/>         <jsp:setProperty name="item1" property="value"                          value="Home" />         <tiles:add beanName="item1"/>     </tiles:putList> </tiles:insert> 
end example

Listing 13.7 uses jsp:useBean to create an instance of SimpleMenuItem. Then it uses jsp:setProperty to set the link and value properties of the SimpleMenuItem bean. Finally, it uses tiles:add to add this bean to the list.

In this example, we added a SimpleMenuItem, which subclasses the MenuItem that our Tile layout uses. However, you can add any bean type.

start sidebar

To add any bean type in the Tiles XML definition, you can use the putList's subelement bean. The bean element takes an id and a classtype attribute. For simple types, you can use putList's subelement add as well. See the Tiles configuration DTD (tiles-config_1_1.dtd) for more information.

end sidebar



 < 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