Joshua Java

Why you should consider Tapestry 5

Posted on: August 21, 2008

Tapestry 5.0.14 has been out recently and the Tapestry community is very excited with this news because Tapestry 5 is only one step away for being GA. In case you haven’t heard of T5, it’s an opensource component based web framework which is a total revamp from the previous version. Some people may not like the previous version because of the learning curve on using it. Some people even wrote rants about the previous version. But T5 is totally a different architecture with the same way of thinking as the previous version. So what’s so good about T5? And why should you consider it for your next project? Here’s my personal reason based on my experience building my opensource project using T5.

  1. Create custom components easily and quicker
  2. Think about creating custom components with Taglib, or even worse think about creating custom components with JSF. Forget about those scary days, now creating components with T5 is so much fun because basically it is just a plain POJO, which means you can unit test your custom components. Here’s a simple example of a T5 component:

    public class AddChar {
      @Parameter(required=true)
      private String value;
    
      @Parameter(required=true)
      private int position;
    
      @Parameter(required=true)
      private String character;
    
      void beginRender(MarkupWriter writer){
        StringBuffer sb = new StringBuffer(value);
    
        sb.insert(position, character);
    
        writer.write(sb.toString());
      }
    }
    

    And here’s how we call it from our page template:

    <t:addChar value="literal:HelloWorld" position="5" character="literal:-"/>
    

    Or if you prefer to embed it in your html tag so your webdesigner can use his/her favorite html designer to preview it, you can use it as such:

    <span t:type='addChar' t:value="literal:HelloWorld" position="5" character="literal:-"/>
    

    Isn’t that too easy to be true? Okay okay, I know it’s a very simple component, but don’t think of the complexity, but think of how many files artifacts you have to manage compared to making components in Struts2, Tapestry 4, JSTL, or even JSF. Whoops, I didn’t mean to bring a web framework bashing forum here but I need to mention the name of the framework so you’ll get the idea what I’m talking about. I’m the kind of guy that really likes creating component, and back in the past creating custom components with frameworks with Struts really frustrates me. But now with T5, I found it very amusing.

  3. Easy to create templates
  4. How do we create templates in T5? (The tapestry community calls this as Layout or Border, a term inherited from Tapestry 4. But I’d just call it as templates as more people are more familiar with the term). You’ll be surprised that all you need to do is create a dummy component (as I call it) under the components package as such:

    @IncludeStylesheet( "context:css/myapp.css" )
    public class Layout {
    
    }
    

    Then create the template with the same name as the component class (Layout.tml) as such:

    <html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
        <head>
            <title>Tapestry5 Experiments</title>
        </head>
        <body>
    <div id="wrap">
            <t:body/></div>
    </body>
    </html>
    

    The <t:body> element really tells you to insert the body of your page fragment. And this is how your page fragment would look like:

    <t:layout xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
    <div id="right">
          Hello</div>
    </t:layout>
    

    And below is the page class where you normally write down your logic:

    public class Search {
    
    }
    

    In an action based framework this is what is called as Controller where you write down your front end logic. And yes, that’s it. Just believe it. Everything is very natural and from my experience it can really help you speed up the development performance. Forget about third party components like sitemesh or tiles just to have templating in your project and the number of file configurations you have to manage.

  5. Less XML configurations to be maintained and configured
  6. Many of the configurations is configured inside web.xml and the rest lies inside a Module class inside the services package. This approach is much better because by putting configurations inside java classes you are assured to get typesafety accross your applications. Here’s an example of the configuration inside web.xml:

    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
             http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
             version="2.5">
    
        <display-name>Tapestry5 Experiments</display-name>
    
        <context-param>
    <param-name>tapestry.app-package</param-name>
    <param-value>yourpackage</param-value>
        </context-param>
        <context-param>
    <param-name>tapestry.production-mode</param-name>
    <param-value>false</param-value>
        </context-param>
    
        <filter>
            <filter-name>app</filter-name>
            <filter-class>org.apache.tapestry5.TapestryFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>app</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
    </web-app>
    

    web.xml will be the only xml file you have to manage with T5. Most of the configuration of T5 apps will be configured inside a Java class. The Java class for configuration will be AppModule.java as the name is taken from the filter name configured in web.xml and the content of the Java class will be as such:

    * Note: this is a default configuration that you will get when you create the project using maven archetype

    public class AppModule
    {
        public static void bind(ServiceBinder binder)
        {
            // binder.bind(MyServiceInterface.class, MyServiceImpl.class);
    
            // Make bind() calls on the binder object to define most IoC services.
            // Use service builder methods (example below) when the implementation
            // is provided inline, or requires more initialization than simply
            // invoking the constructor.
        }
    
        public static void contributeApplicationDefaults(
                MappedConfiguration<String, String> configuration)
        {
            configuration.add(SymbolConstants.SUPPORTED_LOCALES, "en");
            configuration.add(SymbolConstants.PRODUCTION_MODE, "false");
        }
    }
    

    I found it very helpful to have your configuration in a Java class as you can find problems earlier. Also in an more advanced usage, this way of configuration in Tapestry 5 can lead you into building a modular and plug & play components.

  7. Convention over configuration
    1. The Page classes are all located under yourpackage.pages
    2. The Component classes are all located under yourpackage.components
    3. The Service classes are all located under yourpackage.services
    4. and if you use T5-hibernate all of the Model classes are located under yourpackage.entities
  8. Well it’s not like CoC as in Rails, but at least it helps us cuts down things that can be a convention instead of using a configuration. And it does really helps during development.

    The reason why XML configurations has got less XML configurations because many of the configurations is addressed through CoC. How does T5 implement CoC?

    yourpackage gets its values by setting this configuration:

    <context-param>
    <param-name>tapestry.app-package</param-name>
    <param-value>yourpackage</param-value>
    </context-param>
    

    inside web.xml

  9. Integrate seamlessly with groovy
  10. You don’t need special configuration to be able to use groovy in your T5 apps. Just use it and T5 will recognize your .groovy files straight away. Use it anywhere you want: As your Page class or Service class or even DAO class. And why is this important? Because with Groovy you can cut down some boiler plate code.

  11. Built in pretty URL
  12. You do not need a urlrewritefilter framework just to get pretty URL or RESTful URL with T5, because it’s a built in feature without any further configuration. The URL will be generated by the framework based on the structure of your Page class under yourpackage.pages package. So if you have a View class under department package:
    |- yourpackage
    |– department
    |— Add.java
    The URL that will be generated is: http://localhost:8080/yourapp/department/add

    This is a big leap forward because the previous version, T4 has got a really ugly URL by default, and you must configure it to get the pretty URL.

  13. Built in Ajax
  14. Ever wonder you’d want a validation on the client side in an AJAX way but integrate it in an easy fashion? No worries, since this AJAX validation is built right in. And what’s even great it is like an event bubbling where everytime there’s a mistake in the form input, it will point at you the field where there’s a mistake. As far as I know that not many JVM web framework has this feature out of the box, in Seam you have to combine it with a s:decorator to get the Ajax validation feature and in the end it will make your code look verbose.

    Tapestry 5 also lets you return a partial fragment to your page by using the zone component. Another interesting Ajax feature in Tapestry 5 is the ‘google suggest’ like component where you can return result based on what you type in your text input. Under the hood, T5 is using Scriptaculous and Prototype as its Ajax framework. To get a better idea about the Ajax features in T5, you can read it from the documentation here.

  15. Lots and lots of interesting built in components
  16. Besides the Ajax components that I told you in previous point, there are many built in components in T5. Many times you’re found not having to create your own component because the ones that is provided by T5 might be better or just the ones you need.

    One built in component that I’ve found very interesting is the Grid component. The Grid component is where you display your list of data in table. What makes it interesting is, it has built in sorting and pagination feature. Normally with other framework you have to implement this by yourself, or even use a displaytag library. In Flex, I found it frustrating because it hasn’t got pagination feature for the DataGrid component. Now do you want to see how much code is required to make a datagrid that has pagination and field sorter header?

    <t:grid source="users" row="user" rowsPerPage="5"/>
    

    Well, believe it. To get the idea of the built in component that T5 has, check it out from the documentation here.

  17. Seamless integration with Hibernate
  18. The integration with Hibernate makes T5 looks like a full stack web application framework. It has the declarative transaction annotation just like the @Transactional annotation in Spring or Seam. In T5 you just annotate your method with @CommitAfter to get the declarative transaction feature.

  19. Seamless integration with Spring Framework
  20. Even though Tapestry 5 has built in Dependency Injection, but somehow you want to use Spring and inject Spring beans into your T5 apps. You can’t imagine how easy it is to do this in T5.
    You need to change the filter name to use the TapestrySpringFilter in web.xml class as such:

     <filter>
        <filter-name>app</filter-name>
        <!-- Special filter that adds in a T5 IoC module derived from the Spring WebApplicationContext. -->
        <filter-class>org.apache.tapestry5.spring.TapestrySpringFilter</filter-class>
      </filter>
    

    And Tapestry 5 will recognize your Spring bean and you can inject it with T5’s @Inject annotation in your Java class as such:

    @Inject
    private UserDAO userDAO;
    

    Okay basically what that code snippets does is that it will inject your Spring bean that is called UserDAO. It’s that simple.

  21. It just gets better
  22. For the last reason, you may consider it or you may not. Just consider it as a bonus from me. Okay, some people just hates frameworks that just keeps changing almost every single day. But what if I tell you that T5 keeps changing or should I say evolving into something better and better every single day? This can only happen because of the vision of the man behind this framework. The groovy integration that I’ve told you was added recently. And the next thing coming up is the Spring Web Flow integration. Another thing you would be surprised of is though many people have used this framework in their production environment but the author still consider this framework as a Beta. So you’ll get the idea how good it will be when it becomes a GA release.

From my point of view, T5 is filling the space in application framework the same way what Spring framework and Seam is doing. It has everything you need out of the box. I think Tapestry 5 appearance to the surface is great for the community as it adds more choice for the developers to use the relevant framework for their project. What I’ve written is just a small portion of the goodness of T5 and of course it’s what I liked most about T5. The list can go on, as I said it just keeps getting better everyday. But don’t just take my word for it, go ahead and take a look at it. You can start by reading the documentation first to get the overall idea of T5. Hopefully you can find yourself among many other developers that have enjoyed using it since it is first released to the community around a year ago.

18 Responses to "Why you should consider Tapestry 5"

Hi, could we republish this interesting blog entry on Javalobby (java.dzone.com)? if so, please drop me an e-mail at geertjan[at]dzone[dot]com.

Hi Joshua,

good post! I am interested in how do you create this boxes with code. What tools do you use to prepare such nicely formatted java code and xml and the three action links above each code excerpt?

Do it support back button for Ajax ? I.e. if I have a
page and I change the content of fragments/zones
of this page then fire the back button of the browser.
Will this work out-of-the-box ?

[…] Why you should consider Tapestry 5 Tapestry 5.0.14 has been out recently and the Tapestry community is very excited with this news because Tapestry 5 is […] […]

[…] Why you should consider Tapestry 5 « Joshua Java […]

It is clear it is very good for reducing time developing, how is the speed performance.

Can you said tapestry 5 is fast ?

Hi Martin,

What do you expect when you fire the back button of the browser? Could you elaborate that please.

Hi Raul,

As for me T5 is quite fast, though I haven’t done any benchmark with other JVM web framework.

[…] Why you should consider Tapestry 5 (em inglês), artigo por Joshua Java, 2008-08-21. Veja também a discussão do artigo em TheServerSide.COM. A versão 5.0.14 do framework open source de apresentação web Tapestry foi recentemente lançada e o Tapestry 5 está quase atingindo o nível de release estável (GA). A nova geração do Tapestry, criado e mantido pelo contundente Howard Lewis, vem se mostrando cada vez mais sólida e madura, buscando superar algum criticismo contra as mudanças profundas e quebras de compatibilidade retroativa introduzidas na evolução pelas versões 3, 4 e 5 do Tapestry. […]

Hi Joshua,
Have you tried Apache Wicket? You can keep your HTML devoid of any extra tags i.e. <t:layout
except the wicketids.

Can you tell me some advantages over apache Wicket?
I know Tapestry have more no of followers. But which one is more easy to learn

Thanks,
Ketan

Hi Ketan,

Yes I’ve tried and used Apache Wicket in one project before but it failed addressing several issues. Using wicket is more like Swing programming for me. As for me I have to get the mindset first before trying out on Wicket. If you’re a Swing programmer, you wouldn’t have any trouble on learning it. I’m a fan of creating custom component and for me creating custom component in T5 is much easier. Perhaps I’ll create another blog entry on T5 and Wicket one day 🙂

Thanks a lot for the pingback. I surely will join the Ubuntu Weblogs planet.

Ketan,

You can do this with T5 as well, just put a t:id= on the tag and then define all of the other parameter bindings in the java using an annotation.

Josh,

Really nice article. I never been real digg deeply into any web framework before. After reading some discussion in JUG, read your article and T5 documentation, I think I’m gonna get my feet wet with T5 soon.

Thanks for the article.
“mantab kali bah!”

try grails
(www.grails.org) 🙂
lot easier … with powered with groovy spring hibernate and quartz (+ lots of plugins)

[…] On my previous Tapestry 5 article I mentioned how difficult it is to create a custom component with Struts 2 compared to Tapestry 5. […]

[…] 推荐阅读整篇文章:https://joshuajava.wordpress.com/2008/08/21/why-you-should-consider-tapestry-5/ […]

Leave a reply to adwin Cancel reply