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.
blog comments powered by Disqus |
|
|
|
|
|
|
|