In our GuessTheWord project we need to somehow pass the secret word submitted by the user to the Secret page, where the word will be checked, and depending on the result, an appropriate part of the page will be shown. The Home page class has a property to store the submitted word. Before passing the word to the Secret page, we need to configure a similar property in that page too. As you already know, to have a property configured automatically by Tapestry, all we need to provide is an abstract getter method. So let’s add to the Secret class this line of code: public abstract String getTheWord(); However, as you will see soon, we are going to need a setter method too, as we are going to set the value for this property from the Home page. Nothing can be easier; let’s just add an abstract setter too, so that the Secret class will contain the following code: public abstract class Secret extends BasePage {
public abstract String getTheWord(); public abstract void setTheWord();
} Now we can return to our listener method in the Home class. First of all, we need to somehow obtain in it a reference to the Secret page; then we are going to use this reference to pass the value of the secret word. But how can we get a reference to a different page class? IRequestCycle will help us. To every user, Tapestry assigns a kind of majordomo – an object that carries the burden of the application’s housekeeping. All that we need to know about this object as Tapestry developers is that it implements the IRequestCycle interface with a number of useful methods in it. In particular, it can give us a reference to any page of our application. To tell Tapestry that we are going to need help from the majordomo, we simply pass an implementation of IRequestCycle as a parameter of our listener method: … onWordSubmit(IRequestCycle cycle) { … } And then we use the getPage() method to obtain a reference to the desired page: Secret nextPage = (Secret) cycle.getPage(“Secret”); The getPage() method has no idea of the type our Secret page class, so it returns a generic Tapestry page (technically, an implementation of IPage interface, and this interface is always implemented by any Tapestry page). But we know that the page named “Secret” has a class named Secret for its page class, so we can safely cast the returned result to what we expect it to be. The next step is to pass the value of the secret word to the Secret page: nextPage.setTheWord(getTheWord()); Or you can write it like this for clarity: nextPage.setTheWord(this.getTheWord()); Finally, we need to ask Tapestry to show the next page. The simplest way to do this is to return the reference to the page we have just used to set the value. Technically, we return an IPage which simply means “some Tapestry page.” Here is what our completed listener method will look like: public IPage onWordSubmit(IRequestCycle cycle) {
Secret nextPage = (Secret)cycle.getPage("Secret"); nextPage.setTheWord(getTheWord());
return nextPage;
} Run the application, and it should work exactly as before, i.e. both views are displayed by the Secret page simultaneously: the one for a wrong word and the one for the correct word. Now it is the time to complete the application and to meet a few new components while doing this. There is yet another kind of listener in Tapestry, the one that receives a user defined parameter, but let me delay its discussion until we come to the DirectLink component in one of the later parts of the tutorial.
blog comments powered by Disqus |
|
|
|
|
|
|
|