Monday, January 17, 2011

Tapestry Introduction

Tapestry5 represents the current trend in web frameworks to move the paradigm of programming from request response processing to more object oriented and event driven programming.Tapestry promotes a pure html based web application development so that role of web designers and programmers is better managed.

Before moving further download the stable version of 5.0.x of Tapestry from

Make a web application project in your Ide. If you are on eclipse you can make a Dynamic Web Project.Bring the libs available inside tapestry download. All the libs are more than required but at the moment let's go ahead with it.

Now open your web.xml and modify it as follows:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>HelloWorld</display-name>
    <context-param>
        <!--
            This will tell where to look for tapestry pages
        -->
        <param-name>tapestry.app-package</param-name>
        <param-value>com.oyejava.tapestry</param-value>
    </context-param>
    
    <filter>
        <filter-name>tapestryFilter</filter-name>
        <filter-class>org.apache.tapestry5.TapestryFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>tapestryFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

Tapesty register s filter and traps all the request. Also tapesty.app-package tells tapestry to where to look for component Java files. Now put a Index.tml file inside WebContent (The context root if you are on other than eclipse).

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
    <head>
        <title>Tapestry tutorial</title>
    </head>
    <body>
        <h1>Tapestry tutorial</h1>
        <p> Welcome to Tapestry Tutorial </p>
        <p> ${message} </p>
    </body>
</html>

Note that it's a simple but well formed html. The only dynamic part is $message which is resolved by looking into the components file. The component file should be named as Index.java in this case. Index comes from the Index.tml file name after stripping tml. The java file has to put in the tapestry.app-package (from web.xml) location after putting a subpackage pages. So the whole package in this case where Index.java will go is com.oyejava.tapestry.pages

Now Index.java

package com.oyejava.tapestry.pages;

public class Index {
    
    public String getMessage(){
        return "Hello Tapestry";
    }
}

Deploy the application in your container and hit the application at  http://<container url>/<context>

No comments:

Post a Comment