HomeApache Page 4 - PropertySelection and IPropertySelectionModel in Apache Tapestry
One More Detail - Apache
We already encountered PropertySelection in one of the previous articles so we know that it is a Tapestry component used to display a drop-down list, allowing the user to choose one of multiple options. You might think that the way in which options are provided to this component (through its model binding) is somewhat cumbersome when all you need to do is select one of a few strings. However, PropertySelection was designed with a great deal of power and flexibility in mind, and I hope that today you will appreciate this.
Is everything ready now? Not yet. Try to run the application, and when the Home page appears for the first time, you will see something that is not very nice:
AS soon as the application started, the Home page requested an instance of the Celebrity class to be injected into it as a celebrityOfTheWeek ASO. Tapestry created such an instance and gave it to the Home page. However, this instance is empty; all its properties, including firstName and lastName have their default values, null. Hence the result.
What we actually want is to show the celebrity of the week only if it was already chosen. This sounds like we need an If component. But what will be the criterion of whether the ASO was filled in with proper values or not? Let's say the first name should have some value stored in it. All we need to do is surround the fragment of the Home.html template displaying the celebrity of the week with an If component, like so:
Note that I am intentionally mixing together implicit and declared components. This might be not the best approach in a real life application, but here it gives me an opportunity to show you both ways. Also, you have a chance to develop a feeling for where your personal threshold between implicit and declared components lies.
If you run the application now, it should work properly. When the Home page is displayed for the first time, it has no information about the Celebrity of the Week on it. Go to the list of Celebrities, grab the glorious title attached to one of them, and you'll see his or her name appearing on the Home page.
If you now navigate to the CelebritiesList page again, you'll see that the celebrity you've just selected appears as a default choice in the drop-down list. This means that the connection between the PropertySelection component on the page and an ASO in the session on the server remains active.