Friday, May 25, 2012

Tapestry Hello World - Setup

Let us setup a Tapestry project using Maven.

In Eclipse, go to File --> New Project --> Maven Project

Use the maven-archetype-webapp

Specify:
Group Id: org.confucius
Artfact Id: HelloWorldTapestry
Package: org.confucius

Once the project is created, we will update the pom.xml to include the Tapestry libraries.

So your pom.xml will now look like this:
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
       <modelVersion>4.0.0</modelVersion>  
       <groupId>org.confucius</groupId>  
       <artifactId>HelloWorldTapestry</artifactId>  
       <packaging>war</packaging>  
       <version>0.0.1-SNAPSHOT</version>  
       <dependencies>  
           <dependency>  
                <groupId>org.apache.tapestry</groupId>  
                <artifactId>tapestry-core</artifactId>  
                <version>5.3.3</version>  
           </dependency>              
           <dependency>  
                <groupId>org.apache.tapestry</groupId>  
                <artifactId>tapestry5-annotations</artifactId>  
                <version>5.3.3</version>  
           </dependency>  
       </dependencies>  
       <build>  
        <finalName>HelloWorldTapestry</finalName>  
       </build>  
 </project>  

Next, we need to update our web.xml to use Tapestry.

Your web.xml will now look like this:
 <!DOCTYPE web-app PUBLIC  
  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  
  "http://java.sun.com/dtd/web-app_2_3.dtd" >  
 <web-app>  
   <context-param>  
     <param-name>tapestry.app-package</param-name>  
     <param-value>org.confucius</param-value>  
   </context-param>  
   <filter>  
     <filter-name>Tapestry Filter</filter-name>  
     <filter-class>org.apache.tapestry5.TapestryFilter</filter-class>  
   </filter>  
   <filter-mapping>  
     <filter-name>Tapestry Filter</filter-name>  
     <url-pattern>/*</url-pattern>  
   </filter-mapping>  
 </web-app>  

We direct all URLs to be handled by the Tapestry filter.


The default Welcome page for Tapestry is Index.tml

Under /src/main/resources, create the folder structure:
/src/main/resources/org/confucius/pages

In /src/main/resources/org/confucius/pages, create a file Index.tml, like this:
 <html>  
 <body>  
 <h2>Hello World, Tapestry!</h2>  
 </body>  
 </html>  

Note that an TML file looks just like a HTML file.

Under /src/main, create a Source Folder called 'java'
(R-click on /src/main, select New-->Other, then select Java-->Source Folder)


Under /src/main/java, create the folder structure:
/src/main/java/org/confucius/pages


In /src/main/java/org/confucius/pages, create a class Index.java, like this:
 package org.confucius.pages;  
 public class Index {  
 }  

Note that this is an empty POJO, but it must be available to Tapestry - it is the matching POJO for Index.tml

Our Tapestry project is now setup.

If you build and deploy the project, and browse to
http://localhost:8080/HelloWorldTapestry/

You will see "Hello World, Tapestry!" displayed. 

No comments: