Home arrow Apache arrow Page 2 - Making a CelebrityCollector with Apache Tapestry: the For Component

Creating an object model - Apache

We are going to start a new project today, named CelebrityCollector. At first it will be very simple, but in the following articles we’ll be adding more and more functionality to it. Of course, the purpose at this stage of study is not to build a real-world application but to meet different Tapestry components and to learn various important concepts.

TABLE OF CONTENTS:
  1. Making a CelebrityCollector with Apache Tapestry: the For Component
  2. Creating an object model
  3. Creating pages
  4. The For component
By: Alexander Kolesnikov
Rating: starstarstarstarstar / 30
May 09, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

First of all, we need some class to represent a celebrity; it would be natural to name it Celebrity. For the first iteration of the project, this class will have only five properties:

  • A numeric ID (an int);
  • First name (a String);
  • Last name (a String);
  • Date of birth (a Date);
  • Occupation (a String).

We need to have this numeric ID because, well, theoretically speaking it is not against the laws of nature to have two celebrities named John Smith born on the same day who became famous in the same occupation. But even if that happened, we would have this ID as a way to distinguish between them. In a real life application, this could be a primary key in a database.

Create a new class named Celebrity (right-click the earlier created com.devshed.tapestry.celebrities package, New -> Java Class…) and add to it five private members:

private int id;

private String firstName;

private String lastName;

private Date dateOfBirth;

private String occupation;

Again, make use of this handy shortcut: after typing “Date” press Ctrl-Space and select java.util.Date class.

Then allow NetBeans to create getter and setter methods: right-click somewhere in your code and choose Refactor -> Encapsulate Fields… Make sure that everything is selected as the picture shows:

Press Next, and NetBeans will create an overview of the changes it is going to make and show it in the Refactoring view below the code editor. Just press Do Refactoring there, and you will see getters and setters added to the Celebrity class.

Now, we need to have a source of data that will give us a number of Celebrity objects. In a real life application, this would be a database, but for now let’s leave everything as simple as possible and use a mock data source – a simple Java class that creates a List of Celebrity objects and provides methods to retrieve either the whole list or just one specified object. Later, we might replace the mock data source with a real one.

Let’s add a DataSource class to the existing Java package. There is nothing Tapestry-specific or terribly clever in this class, so you can simply copy the code:

package com.devshed.tapestry.celebrities;

 

import java.util.ArrayList;

import java.util.Calendar;

import java.util.Iterator;

import java.util.List;

 

public class DataSource {

   

    List list = new ArrayList();

   

    /** Creates a new instance of DataSource */

    public DataSource() {

       

        Calendar calendar = Calendar.getInstance();

       

       

        Celebrity fonda = new Celebrity();

        fonda.setFirstName("Jane");

        fonda.setLastName("Fonda");

        fonda.setOccupation("Actress");

        calendar.set(1937, 11, 21);

        fonda.setDateOfBirth(calendar.getTime());

        fonda.setId(1);

       

        list.add(fonda);

       

        Celebrity gallo = new Celebrity();

        gallo.setFirstName("Ernest");

        gallo.setLastName("Gallo");

        gallo.setOccupation("Wine-maker");

        calendar.set(1909, 2, 18); // Months are 0-based (2 is March)

        gallo.setDateOfBirth(calendar.getTime());

        gallo.setId(2);

       

        list.add(gallo);

       

        Celebrity gates = new Celebrity();

        gates.setFirstName("Bill");

        gates.setLastName("Gates");

        gates.setOccupation("Programmer ;)");

        calendar.set(1955, 9, 28);

        gates.setDateOfBirth(calendar.getTime());

        gates.setId(3);

       

        list.add(gates);

       

        Celebrity jolie = new Celebrity();

        jolie.setFirstName("Angelina");

        jolie.setLastName("Jolie");

        jolie.setOccupation("Actress");

        calendar.set(1975, 5, 4);

        jolie.setDateOfBirth(calendar.getTime());

        jolie.setId(4);

       

        list.add(jolie);       

       

    }

   

    public List getCelebrities() {

        return list;

    }

   

    public Celebrity getCelebrityById(int id) {

       

        for (Iterator i = list.iterator(); i.hasNext();) {

            Celebrity celebrity = (Celebrity)i.next();

            if (celebrity.getId() == id) return celebrity;

        }

       

        return null;

    }      

}

This class simply creates a List of a few celebrities in its constructor using some hard-coded data. We can obtain the whole lot by using the getCelebrities() method, or retrieve a single celebrity by specifying his or her ID and using the getCelebrityById() method.

Now that we have all supporting code ready, we can begin converting page mock ups into Tapestry templates.



 
 
>>> 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: