Apache
  Home arrow Apache arrow Page 4 - Making a CelebrityCollector with Apache Tapestry: the For Component
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
APACHE

Making a CelebrityCollector with Apache Tapestry: the For Component
By: Alexander Kolesnikov
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 25
    2007-05-09


    Table of Contents:
  • Making a CelebrityCollector with Apache Tapestry: the For Component
  • Creating an object model
  • Creating pages
  • The For component

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Making a CelebrityCollector with Apache Tapestry: the For Component - The For component
    ( Page 4 of 4 )

    The For component does three crucially important things:

    1. It takes a bunch of objects and iterates through them one by one. Technically, this bunch can be an Array or any representatives of the Collection interface, such as an ArrayList or a HashSet, to name a few.
    2. Each time it picks an object from the bunch it makes that object available for our code.
    3. It displays any markup and any components surrounded by itself as many times as there are objects in the bunch.

    Let’s begin from configuring a For component in the List page specification:

    <component id="eachCelebrity" type="For">

       <binding name="source" value="celebrities"/>

    </component>

    We’ve specified a For component and given it some name (eachCelebrity). We have also provided its required and most important binding, source. This is where the bunch of objects will come from. Let’s define the getCelebrities() method in the CelebritiesList page then:

    public abstract class CelebritiesList extends BasePage {

       

        public List getCelebrities() {

            return new DataSource().getCelebrities();

        }

       

    }

    We can place a For component in HTML markup in the same way as we did this before, using a div or a span. However, in this specific case it is more reasonable to convert a table row (tr element) into a For component. Let’s see how we can do this:

    <table width="300" cellpadding="5" border="1">

        <tr>

            <th>Last Name</th>

            <th>First Name</th>

        </tr>

        <tr jwcid=”eachCelebrity”>

            <td>

                <a href="">Smith</a>

            </td>

            <td>John</td>

        </tr>

        <tr jwcid=”$remove$”>

            <td>

                <a href="">Smithson</a>

            </td>

            <td>Jane</td>

        </tr>

        <tr jwcid=”$remove$”>

            <td>

                <a href="">Swedenborg</a>

            </td>

            <td>Emmanuel</td>

        </tr>

    </table>

    We have marked one of the table rows as an “eachCelebrity” component which we have just defined in the page specification. It should be repeated as many times as there are Celebrity objects in the bunch provided to this component. But now we have two table rows which we don’t want to see at runtime; they were written by the designer for preview purposes only.

    Quite conveniently, Tapestry has a special thing which looks like a component ID but in fact just marks an HTML element for removal at runtime: jwcid=”$remove$”. So we have marked the two remaining table rows for removal.

    Run the application, and this is what you should see:

    It works! The rows for Jane Smithson and Emmanuel Swedenborg were removed, as required, and the row for John Smith was repeated four times – exactly the number of celebrities in our mock data source. Now we need to display the real celebrities instead of John Smith.

    First of all, we shall ask the For component to expose to our code the Celebrity object it is currently iterating through. We use another binding for this purpose:

    <component id="eachCelebrity" type="For">

        <binding name="source" value="celebrities"/>

        <binding name="value" value="currentCelebrity"/>

    </component>

    Each time our For component picks a Celebrity object from the List provided to it, it will pass this object to the page class by invoking the page class’ setCurrentCelebrity() method. We don’t have such a method at the moment and we don’t need to write it by hand. All we need to do is tell Tapestry to create a currentCelebrity property.

    Normally, we would simply provide an abstract getter method, and Tapestry would do the rest. This time however we are not going to access the new property from our Java code. As you will see in a moment, it will be used only by components on the page. So it makes sense to specify the property in the page specification:

    <page-specification
            class="com.devshed.tapestry.celebrities.CelebritiesList">

     

        <property name="currentCelebrity"/>

       

        <component id="eachCelebrity" type="For">

            <binding name="source" value="celebrities"/>

            <binding name="value" value="currentCelebrity"/>

        </component>

      

    </page-specification>

    Finally, we are going to use a couple of Insert components to display the first and last name of the current celebrity in the corresponding table row:

     <tr jwcid="eachCelebrity">

          <td>

              <a href="">

                  <span jwcid="lastName">Smith</span>

              </a>

          </td>

          <td>

              <span jwcid="firstName">John</span>

          </td>

      </tr>

    Since we decided to use declared components here, we need to define them in the page specification:

    <component id="firstName" type="Insert">

        <binding name="value" value="currentCelebrity.firstName"/>

    </component>

    <component id="lastName" type="Insert">

        <binding name="value" value="currentCelebrity.lastName"/>

    </component>

    Run the application, click on the “List Celebrities” link, and you should see the correct result:

    However, if you click on a link in the table, nothing will happen, while we would like to see the Details page for the selected celebrity. To implement this functionality, we need a DirectLink component, and this is exactly what we are going to deal with in the next article.



     
     
    >>> More Apache Articles          >>> More By Alexander Kolesnikov
     

       

    APACHE ARTICLES

    - Creating a VAMP (Vista, Apache, MySQL, PHP) ...
    - Putting Apache in Jail
    - Containing Intrusions in Apache
    - Server Limits for Apache Security
    - Setting Permissions in Apache
    - Installing Apache
    - Apache Installation and Configuration
    - Apache Tapestry and Custom Components: DateI...
    - Tapestry and AJAX: Autocompleter and InlineE...
    - PropertySelection and IPropertySelectionMode...
    - The DatePicker and Shell Components of Apach...
    - Apache Tapestry: ASO and More Components
    - Apache Tapestry and DirectLink, IoC and DI
    - Making a CelebrityCollector with Apache Tape...
    - Apache Tapestry and Listener Methods, Condit...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek