Home arrow Apache arrow Page 3 - Apache Tapestry and Listener Methods, Conditional Components and PageLink

Conditional components - Apache

Today we continue to explore some of the most fundamental concepts of Tapestry while building one of the simplest Java Web applications. In the previous part of this tutorial we became familiar with properties of Tapestry pages and different ways to configure them. Now we’ll look into the details of writing listener methods.

TABLE OF CONTENTS:
  1. Apache Tapestry and Listener Methods, Conditional Components and PageLink
  2. Passing information to the next page
  3. Conditional components
  4. PageLink component
  5. Deploying the application
By: Alexander Kolesnikov
Rating: starstarstarstarstar / 26
May 08, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

We want to show the hidden truth only if the secret word was guessed properly. Wouldn’t it be natural to use the Tapestry’s If component for this purpose? The If component has one binding, the condition to evaluate, but since our Secret page is very simple in its design, I think it will be okay to define this component implicitly, like so:

<div jwcid=”@If” condition=”ognl:theWord == ‘abrakadabra’”>

   <!—Stuff to show if everything is OK --/>

</div>

Previously, we have used a span HTML element as a placeholder for our Tapestry component. Now we use div for the same purpose, as it is more logical to surround a block of markup with a div. The If component will simply show or not show the contents of the div depending on whether its condition evaluates to true or to false.

We have used the ognl prefix to specify that the value for the condition attribute should be treated as an OGNL expression. If we forget to do this (which I do quite often) the value will be treated verbatim and then anything non-empty will be understood as true.

Then we are comparing the word that was passed from the Home page with the hard-coded word “abrakadabra” (you can use any other word of course!). The word to compare with is put into single quotes as double quotes are already used to surround the value of the condition attribute. Of course, hard-coding anything like this would not be a good idea in any real-life application, but we’ll leave it just like that for now, for simplicity’s sake.

So, when the user enters the magical word “abrakadabra” at the Home page and presses the Submit button, the hidden truth is displayed by the Secret page, but if the user enters anything else… The Tapestry’s Else component will take care of displaying the alternative content.

The Else component is very simple, it doesn’t require any bindings, and so it is natural to define it implicitly, like so:

<div jwcid=”@Else”>

   <!-- An alternative content goes here -->

</div>

Of course, to work properly, an Else component has to follow an If component. Now the HTML template for the Secret page should look like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

 

<html>

  <head>

    <title>Guess Result</title>

  </head>

  <body>

      <div jwcid="@If" condition="ognl:theWord == 'abrakadabra'">

          <!-- This will be shown if the guess was successful -->

          <h2>Congratulations!</h2>

          <p>You have guessed the secret word properly,

          and here is the hidden wisdom:</p>

          <p><i>Lorem ipsum dolor sit amet, consectetur

                  adipisicing elit, sed do eiusmod tempor

          incididunt ut labore et dolore magna aliqua.</i></p>    

      </div>

      <div jwcid="@Else">

          <!-- And this will be shown if case of failure -->

          <h2>This was a wrong word</h2>

          <p><a rel="nofollow" target="_blank" href = "">Go back and try again.</a></p>

      </div>

  </body>

</html>

Run the application, and it should work properly: if you submit the correct word, the hidden truth is shown, otherwise you are prompted to try again.



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