Apache
  Home arrow Apache arrow Page 3 - Apache Tapestry and DirectLink, IoC and DI
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? 
APACHE

Apache Tapestry and DirectLink, IoC and DI
By: Alexander Kolesnikov
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 17
    2007-05-14


    Table of Contents:
  • Apache Tapestry and DirectLink, IoC and DI
  • DirectLink and For working together
  • Passing a parameter
  • Inversion of Control and Dependency Injection

  • 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


    Apache Tapestry and DirectLink, IoC and DI - Passing a parameter
    ( Page 3 of 4 )

    As it is usual with Tapestry, it takes care of low level details, so all we need to do is tell that we expect to have some parameter in the listener, like so:

    public IPage onShowDetails(int id) {

     

       // Do something

     

       return null;

    }

    Could it be simpler? Now, let's go on and complete the application. One minor enhancement: as we are going to use the DataSource class more than once, let's save its instance so that we don't have to instantiate it every time. Add the following line of code to CelebritiesList.java:

    private DataSource dataSource = new DataSource();

    And then amend the getCelebrities() method:

    public List getCelebrities() {

       return dataSource.getCelebrities();

    }

    Back in the listener method, we can obtain the Celebrity object, as selected by the user, by its ID, from the DataSource:

    public IPage onShowDetails(int id) {

     

       Celebrity celebrity = dataSource.getCelebrityById(id);

       // Pass the celebrity to the Details page

     

    return null;

    }

    Now we need to get a reference to the Details page. In one of the previous issues you have seen one way to do this, through IRequestCycle. There is, however, a nicer way to do the same. Add these two lines of code to CelebrityList.java:

    @InjectPage("Details")

    public abstract Details getDetailsPage();

    This is an example of so-called injection, and we'll be using a lot of them in Tapestry. We are just saying here that we are going to need a reference to the Details page at some point, so we ask the framework to provide such a reference at runtime.

    As you might guess, this is an example of the super-popular "Dependency Injection." You will find my clarifications on this term, as well as on the other very popular one, "Inversion of Control." at the end of this article. Right now, let's just complete the practical part. Make the code for the CelebritiesList class look like this:

    package com.devshed.tapestry.celebrities;

     

    import java.util.List;

    import org.apache.tapestry.IPage;

    import org.apache.tapestry.annotations.InjectPage;

    import org.apache.tapestry.html.BasePage;

     

    public abstract class CelebritiesList extends BasePage {

     

       private DataSource dataSource = new DataSource();

     

     

       @InjectPage("Details")

       public abstract Details getDetailsPage();

     

     

       public List getCelebrities() {

          return dataSource.getCelebrities();

       }

     

     

       public IPage onShowDetails(int id) {

     

          Celebrity celebrity = dataSource.getCelebrityById(id);

     

          Details nextPage = getDetailsPage();

          nextPage.setCelebrity(celebrity);

     

          return nextPage;

       }

    }

    Run the application, go to the list of celebrities and click on someone's surname. You should see something like this:

    The application works okay, but isn't the format used to display the birth date a bit weird? You probably remember that I have promised you in a previous article that we'll take care of how this date is displayed. Actually, the Insert component can do this for us.

    Go to the Details page specification and change the declaration of the birthday component to look like this:

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

       <binding name="value" value="celebrity.dateOfBirth"/>

       <binding name="format" value="dateFormat"/>

    </component>

    Finally, add the getDateFormat() method to the Details.java code:

    package com.devshed.tapestry.celebrities;

     

    import java.text.Format;

    import java.text.SimpleDateFormat;

    import org.apache.tapestry.html.BasePage;

     

    public abstract class Details extends BasePage {

     

     

       public abstract Celebrity getCelebrity();

       public abstract void setCelebrity(Celebrity c);

     

     

       public Format getDateFormat() {

          return new SimpleDateFormat("MMM d, yyyy");

       }

    }

    Now if you run the application everything should appear properly:

    Before going further, we need to become familiar with a few theoretical concepts.



     
     
    >>> 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 1 Hosted by Hostway
    Stay green...Green IT