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.
blog comments powered by Disqus |
|
|
|
|
|
|
|