Tuesday, May 29, 2012

Tapestry - Navigation

Let us see how to navigate from one page to another in Tapestry.

Let us create two new pages.

In your /src/main/resources/org/confucius/pages, create a file called InternationalGreets.tml, like this:

 <html t:type="layout" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">  
      <body>  
         <p>   
       English: Hello World!   
       <br/>   
       French: Bonjour tout le Monde!   
       <br/>   
       Italian: Buongiorno a Tutti!   
       <br/>   
       Spanish: Hola Mundo!   
       <br/>   
       Swahili: Jambo!   
         </p>   
      </body>  
 </html>  
   

In your /src/main/java/org/confucius/pages, create a file called InternationalGreets.java, like this:

 package org.confucius.pages;  
   
 public class InternationalGreets {  
   
 }  
   


In your /src/main/resources/org/confucius/pages, create a file called AmericanGreets.tml, like this:

 <html t:type="layout" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">  
      <body>  
         <p>   
          Regular: Hi!   
       <br/>   
       Friendly: Whatsup!   
       <br/>   
       Extra Friendly: Hey, How are you doing!   
         </p>   
      </body>  
 </html>  
   

In your /src/main/java/org/confucius/pages, create a file called AmericanGreets.java, like this:

 package org.confucius.pages;  
   
 public class AmericanGreets {  
   
 }  
   


Update Index.tml to contain two new buttons, like this:

 <html t:type="layout" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">  
      <body>  
           <h2>${greeting}</h2>  
             
           <form t:id="greetForm">  
                <input t:id="internationalGreetsButton" t:type="submit" value="Show International Greets"/>  
                <input t:id="americanGreetsButton" t:type="submit" value="Show American Greets"/>  
           </form>  
      </body>  
 </html>  
   


Update Index.java to handle the two new buttons, like this:

 package org.confucius.pages;  
   
 import org.apache.tapestry5.annotations.Component;  
 import org.apache.tapestry5.corelib.components.Form;  
   
 public class Index {  
      private String greeting;  
      private Object formEventReturn;  
        
      @Component(id = "greetForm")  
      private Form form;       
        
      public Index() {  
           this.greeting = "Hello World, Tapestry!";  
      }  
   
      void onSelectedFromAmericanGreetsButton() {   
           formEventReturn = AmericanGreets.class;  
      }  
        
      void onSelectedFromInternationalGreetsButton() {   
           formEventReturn = InternationalGreets.class;  
      }  
   
      Object onSuccessFromGreetForm() {  
           return formEventReturn;  
      }  
        
      public void setGreeting(String greeting) {  
           this.greeting = greeting;  
      }  
   
      public String getGreeting() {  
           return greeting;  
      }  
        
 }  
   


Note that we have introduced three new methods, two which handle the button clicks, and one which handles the form submission.

We return the .class corresponding to the appropriate page - this tells Tapestry which page to navigate to.

If you rebuild and redeploy HelloWorldTapestry, you will see two buttons which navigate you to International and American greets.

No comments: