Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Changed to have the first example be a component that does use a template, because it's the simpler and more common case.

...

A Component class is the class associated with a page, component or mixin in your Tapestry web application. Classes for pages, components and component mixins are all created in an identical way. They are pure POJOs (Plain Old Java Objects), typically with annotations and conventionally named methods. They are not abstract, nor do they need to extend from framework base classes or implement interfaces.

For Tapestry 4 Users: Component classes in Tapestry 5 are much easier than in Tapestry 4. There are no base classes to extend from, the classes are concrete (not abstract), and there's no XML file. There is still a bit of configuration in the form of Java annotations, but those now go directly onto fields of your class, rather than on abstract getters and setters.

In most cases, each component class will have a corresponding component template. However, it is also possible for a component class to emit all of its markup itself, without using a template.

Creating a Trivial Component

...

Creating a page and or component classes in Tapestry 5 is a breeze. There are only a few constraints:

  • The classes There must be a public Java class.
  • The classes class must be in the correct package (see below).
  • The class must have a standard public, no-arguments constructor. (The default one provided by the compiler is fine.)

Here's a very basic component's a minimal component that outputs a fixed message, using a template:

Section
Column
Code Block
java
java
titleHelloWorld.java

package org.example.myapp.components;
public class HelloWorld
{
}
Column
Code Block
xml
xml
titleHelloWorld.tml

<t:container xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd">
    Bonjour from HelloWorld component.
</t:container>

In this example the HelloWorld class contains no code at all (except what it inherits from the Object class and what Tapestry adds invisibly).

And here's a component that does the same thing, but without needing a template:

Code Block
java
java
titleHelloWorld.java -- without a template
package org.example.myapp.components;

import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.annotations.BeginRender;

public class HelloWorld
{
    @BeginRender
    void renderMessage(MarkupWriter writer)
    {
        writer.write("Bonjour from HelloWorld component.");
    }
}

In this example, just like the first one, the component's only job is to write out a fixed message. The @BeginRender annotation is a type of component life cycle annotation, a method annotation that instructs Tapestry when and under what circumstances to invoke methods of your class.

...