Apache
  Home arrow Apache arrow Page 4 - Making a CelebrityCollector with Apach...
FaxWave - Free Trial.
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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? 
APACHE

Making a CelebrityCollector with Apache Tapestry: the For Component
By: Alexander Kolesnikov
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 24
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    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.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · In this issue we are beginning to build a new application, and I am going to use it...
       · <property name = "currentCelebrity"/> <component id="eachCelebrity" ...
       · Please see Alexander's previous article for its...
       · public abstract class CelebritiesList extends BasePage { public List...
       · Why not? Count the brackets ;)
       · So, I get the error below. I was wondering if you could please also give some...
       · Here is the HTML code that got left out from above: <tr...
       · Any takers?? Alex?
       · OK, so the porblem seems to be that I can not put a tapestry component in a table...
       · <property name = "currentCelebrity"/>The above is equivalent to creating a...
     

       

    APACHE ARTICLES

    - 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...
    - The Properties of Tapestry Pages

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway