Tuesday, May 29, 2012

Tapestry - Bean-side Components

Tapestry allows you to specify components in the bean, instead of in the template.

Let us create the Sliding Panel component in the bean. To refresh our memory, the bean is the Java class associated with the template.

Update your Layout.java class to include the SlidingPanel component, like this:

 package org.confucius.components;  
   
 import org.apache.tapestry5.annotations.Component;  
 import org.chenillekit.tapestry.core.components.SlidingPanel;  
   
 public class Layout {  
   
      @Component(parameters = {"subject=Chenillekit Greeter"})  
      private SlidingPanel panel1;  
        
 }  
   

Here, we have used annotations to tell Tapestry that 'panel1' is a SlidingPanel component, and its 'subject' property is 'Chenillekit Greeter'.

Now, update Layout.tml to use this Sliding Panel, like this:
 <html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">  
   <head>  
     <title>Tapestry</title>  
   </head>  
   <body>  
     <h3>Basic Tutorial Series</h3>  
     
           <div t:id="panel1">  
                  <t:body/>  
           </div>  
   
           <p>Visit Us @ www.projectconfucius.org</p>  
             
   </body>  
 </html>  


If you rebuild and redeploy HelloWorldTapestry, you will see the Sliding Panel just like before. Thus, there is no difference in presentation whether you specify a component in the template or in code.

Typically, you will specify the component in code when you want to configure it dynamically, or if you want to keep your template clean of configurations.

No comments: