Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: restore

Excerpt
hiddentrue
INLINE

the various annotations and methods you can add to Tapestry page and component classes

Scrollbar

 

This is a summary of the more common annotations and methods you can add to Tapestry pages and component classes.

Div
stylefloat:right
titleRelated Articles
classaui-label
Content by Label
showLabelsfalse
showSpacefalse
titleRelated Articles
cqllabel in ("component-classes","component-templates","components") and space = currentSpace()

For an exhaustive list, see the annotations list.

Field Injection Annotations

Main articles: Component Classes, Injection, AnnotationsCheat Sheet, Component Cheat Sheet, Component Cheat Sheet

@Inject

@Inject is the Swiss Army knife of annotations; it's designed to connect your component to services, resources, and other objects. See Injection Component Cheat Sheet.

Service Injection

In most cases, the injected value is a service; the service is located by type. If there are ambiguities, caused by multiple services implementing the same interface, you'll see injection exceptions. You can resolve those exceptions by adding marker annotations to select a specific service, or by adding @Service to specify the specific service ID you want.

Note

Use of @Service is discouraged. If marker annotations are available, that is preferred.

@InjectComponent

Injects a component from this component's template into this component's class. Injecting a component is based on the component's ID, which should match the field name. However, the value attribute of the @InjectComponent annotation can be specified as well, this takes precedence over the field name.

...

Injects a page of the application. Normally, the page to inject is identified based on the field type. The value attribute can be specified, in which case the page to be injected is identified by name.

@Environmental

Injects an environmental object; such objects are request scoped but may be overridden at any time using the methods of the Environment service. Environmental objects are used to allow outer components to communicate with components they enclose.

...

Field Behavior Annotations

Main articles: Component Classes, AnnotationsCheat Sheet, Component Cheat Sheet

@PageActivationContext

This annotation is allowed on a single field; the value of the field will be included in URLs for the page as the page's activation context. This is an alternative to implementing event handler methods
for the activate and passivate events directly.

...

Marks the field as a component parameter. Attributes of the annotation allow the parameter to be marked as required or optional. If the parameter value will typically be a literal string (for example, the title parameter to a Layout component), you should add defaultPrefix=BindingConstants.LITERAL to the annotation so that users of the component won't have to use the "literal:" binding prefix with the parameter. See Component ParametersCheat Sheet

@Persist

Marks the field as a persistent value, one that maintains its value between requests. The default strategy is to simply store the value in the session (which is created as needed). Other strategies can be specified by name as the value attribute. See Persistent Page Data Component Cheat Sheet.

@Property

Directs Tapestry to automatically generate a getter and a setter for the field, converting it to a JavaBeans property than can be referenced from the template.

...

Marks the field as a Session State Object (SSO). SSOs store global data, and can be injected into any page or component. The SSOs are stored in the session, using a key based on the Java type. SSOs are usually created on demand, but the create attribute can turn this off. See Session Storage Component Cheat Sheet

@SessionAttribute

In Tapestry 5.2 and later, marks the field as a Session Attribute. Like Session State Objects (SSO), a Session Attribute is stored in the session, however Session Attributes are stored by using a name you choose rather than based on the Java type. See Session Storage Component Cheat Sheet.

@ActivationRequestParameter

...

Method Annotations

Main articles: Component Classes, AnnotationsCheat Sheet, Component Cheat Sheet

@OnEvent

Marks a method as an event handler method. Such methods may have any visibility, and typically use package private visibility (that is, no visibility keyword at all). By default, the method will handle the action event from any component; the value attribute controls the matched event, and the component annotation is used to limit the event source.

...

You may also return true to indicate that the event is handled and bubbling should cancel (even for events that do not permit a return value).

Note

An alternative to @OnEvent is the naming convention onEventName or onEventNameFromComponentId.

@Log

Marks the method to be logged for debugging purposes: method entry (with parameters) and exit (with return value) will be logged at debug level, as will any thrown exception. This is primarily for debugging purposes. The Logger name will match the component classes' fully qualified class name.

@CommitAfter

Note

The support for this annotation comes from the tapestry-hibernate module or tapestry-jpa module.

@Cached

Used on methods that perform expensive operations, such as database queries. The first time such a method is invoked, the return value is cached. Future invocations of the same method return the cached value.

...

Parameter Annotations

Main article: Component ParametersCheat Sheet

@RequestParameter

Used with event handler methods to get the value for the parameter from a request query parameter.

...

@Import may also be applied to individual methods, in which case the import operation only occurs when the method is invoked.

Note

When specifying a file to import, you'll often use the prefix context: to indicate that the file is stored in the web application context, and not on the classpath. Relative paths will be on the classpath, relative to the Java class.

@SupportsInformalParameters

...

Render Phase Methods

Main article: Component RenderingCheat Sheet

Render phase methods are close cousins to event handler methods; they are how Tapestry integrates your code into the overall rendering of the page. For each render phase, there's an annotation and corresponding naming convention to define a render phase method:

...

Render phase methods may return void, a boolean, or a renderable object.

Note

Generally, a renderable object is a Block or a component. The object is pushed onto the stack of rendering operations, temporarily replacing the current component as the object to be rendered.

Returning true is the same as returning void; it means that the component should follow the typical flow:

...

A render phase method may also return false, in which case the flow continues to an alternate render phase, as per the chart in the Component RenderingCheat Sheet reference page.

The most common cases:

...

Page Life Cycle Methods

Main article: Page Life Cycle Component Cheat Sheet

Pages have a life cycle and this is represented by a third set of annotations or method naming conventions. Life cycle methods may appear on a page or any component of a page.

...

The SymbolProvider service has two interfaces : FactoryDefaults and ApplicationDefaults. Tapestry provides 2 annotations in order to define which implementation you want to override in your AppModule : 

  • @FactoryDefaults

    Code Block
    titleAppModule with @FactoryDefaults
    @Contribute(SymbolProvider.class)
    @FactoryDefaults
    public void setParam(MappedConfiguration< String, String> configuration){
      configuration.add(SymbolConstants.PRODUCTION_MODE, "false");
    }
  • @ApplicationDefaults

    Code Block
    titleAppModule with @ApplicationDefaults
    @Contribute(SymbolProvider.class)
    @ApplicationDefaults
    public void setParam(MappedConfiguration< String, String> configuration){
      configuration.add(SymbolConstants.PRODUCTION_MODE, "false");
    }

 

Scrollbar