Home arrow Apache arrow Page 3 - The Properties of Tapestry Pages

Resetting page properties by hand - Apache

In the previous part of this tutorial we started to build a new project, GuessTheWord. The project is very simple, but we are going to spend a lot of time working on it and experimenting with it. This is because the main aim is to learn a lot about the most basic concepts of Tapestry. For every important concept, I want to show you a number of options as to how it can be implemented and explain which option is good for what.

TABLE OF CONTENTS:
  1. The Properties of Tapestry Pages
  2. Different ways to manage page properties
  3. Resetting page properties by hand
  4. Configuring a property in page specification
  5. Setting an initial value
By: Alexander Kolesnikov
Rating: starstarstarstarstar / 25
May 07, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

When Tapestry creates a page property automatically, it creates basic getter and setter methods that do nothing but retrieve or set the value of the property. But sometimes we might want to put some functionality into the getter and/or setter methods. In the GuessTheWord application, for example, we might want to do something depending on the length of the word submitted by the user and write a setter like this:

public void setTheWord(String theWord) {

  

   if (theWord != null) {

       int length = theWord.length();

      

       // Do something…

   }

 

   this.theWord = theWord;

}

This might be not be a very convincing example, but believe me, sometimes you just cannot avoid writing some code in a getter or a setter. And as soon as you have provided a getter or a setter of your own, you have to write all the remaining code yourself. So Tapestry will not take care of setting theWord to null; you’ll need to do that yourself too.

In Tapestry 3, page classes have a very convenient method for this purpose, initialize(). In Tapestry 4, this method still exists but it is deprecated. So it is not recommended that we use it, and I should not be mentioning it here. But still, this method is more convenient than the alternative solution and I have to admit that I use the deprecated initialize() method in my work.

It looks like this:           

protected void initialize() {

   theWord = null;

}

As soon as we have provided this method in our page class, it will be called every time the page instance is sent to the pool, so we have achieved what we wanted.

The alternative approach, recommended for use in Tapestry 4, is to implement the PageDetachListener interface and its only method pageDetached(PageEvent event). So if you do everything properly, your Home class will look like this:

public abstract class Home extends BasePage implements PageDetachListener {

 

    private String theWord;

 

    private void setTheWord(String theWord) {

          this.theWord = theWord;

    }   

 

    public String getTheWord() {

        return theWord;

    }

 

    public void pageDetached(PageEvent event) {

        theWord = null;

    }

   

    public String onWordSubmit() {

        return "Secret";

    }

}

For me, it’s just the same result with more coding, but of course the creators of Tapestry know better.



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