Over a million developers have joined DZone.

Why You Should Consider Tapestry 5

DZone's Guide to

Why You Should Consider Tapestry 5

· Java Zone
Free Resource

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

Tapestry 5.0.14 was released 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 {
    private String value;

    private int position;

    private String character;

    void beginRender(MarkupWriter writer){
    StringBuffer sb = new StringBuffer(value);

    sb.insert(position, character);


    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">
    <title>Tapestry5 Experiments</title>
    <div id="wrap">


    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">

    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"

    <display-name>Tapestry5 Experiments</display-name>






    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:


    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:

    <!-- Special filter that adds in a T5 IoC module derived from the Spring WebApplicationContext. -->

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

    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.

From: http://joshuajava.wordpress.com/

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.


Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}