Home arrow Apache arrow Page 3 - Apache Tapestry and DirectLink, IoC and DI

Passing a parameter - Apache

Previously we began building our CelebrityCollector application. But right now it is a hollow shell. In this article we will use several new Apache Tapestry components to help us start filling in the details.

TABLE OF CONTENTS:
  1. Apache Tapestry and DirectLink, IoC and DI
  2. DirectLink and For working together
  3. Passing a parameter
  4. Inversion of Control and Dependency Injection
By: Alexander Kolesnikov
Rating: starstarstarstarstar / 20
May 14, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

APACHE ARTICLES

- Apache Unveils Cassandra 1.2
- Apache on ARM Chips? Dell and Calxeda Help M...
- The Down Side of Open Source Software
- VMware Unveils Serengeti for Apache Hadoop
- SAP Takes Steps to Improve Hadoop Integration
- Looking to Hone Apache Hadoop Skills?
- How to Install Joomla on WAMPP
- Working with XAMPP and Wordpress
- GUI Available for Apache Camel
- Reduce Server Load for Apache and PHP Websit...
- Creating a VAMP (Vista, Apache, MySQL, PHP) ...
- Putting Apache in Jail
- Containing Intrusions in Apache
- Server Limits for Apache Security
- Setting Permissions in Apache

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: