Kontakt

Context Values vs. Request Parameters

Posted on Montag, 23rd August, 2010

In this post I want to show you some new features introduced in Tapestry 5.2. You’ll learn how to access request parameters in a easy way.

As you probably know Tapestry provides a very nice mechanism to pass some data from one page to another. This mechanism is called page activation context. Before a page is rendered Tapestry extracts the context values from the URL, converts the string values to appropriate type and activates the page. Usually a URL to a page looks like the following, whereby 12345 is the page activation context.

/viewuser/12345/

Note that the page activation context is encoded into the request path.

Mapping Fields to Request Parameters

Sometimes you might prefer request parameters over page activation context to pass data to a page. An example is a migration of an existing legacy application to Tapestry. It’s quite common to do this in stages, moving some functionality into Tapestry and leaving other parts, initially, in the other system.  Imagine you want to migrate a single page of your application which is accessed via a URL like:

/viewuser?userId=12345

You can use the @ActivationRequestParameter annotation to map a field of a page (not a component) to a request parameter. When a page is activated, the mapped fields will receive their values from request parameters before onActivate() event handler method is invoked. The annotation value is used to specify the name of the request parameter, which is mapped to the field. If no such value is provided, then the name of the field is taken as the name of the request parameter. For the following page Tapestry will extract the value from the userId request parameter, transform the string value to a User instance and assign the value to the user property.

public class ViewUser {

   @Property
   @ActivationRequestParameter("userId")
   private User user;
}

The assigned value can be accessed in the page template in a usual way:

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">
    <body>
        User: ${user.name}
        <br/>
        <t:pagelink page="viewuser">refresh</t:pagelink>
    </body>
</html>

The @ActivationRequestParameter annotation may be considered as an alternative to @Persist annotation. This annotation tells Tapestry to persist a field value from one request to the next. The main difference to the @Persist annotation is that the value is not stored into a session, onto the client, etc. The value is rescued as a request parameter. When Tapestry generates a component event or page render link for a page, it will identify all fields annotated with @ActivationRequestParameter and add their values as request parameters to the link. Open the ViewUser page in your browser with a valid user id as request parameter and roll your mouse over the refresh link. You will see in the statusbar of your browser that the user id has been appended to the URL of the link automatically.

Accessing Request Parameters in Handler Methods

Alternatively you can use the @RequestParameter annotation to access the request parameters. The main difference is that @RequestParameter annotation may be placed only on method parameters as shown in the following example. The annotation doesn’t persist the request parameter between two requests.

public class ViewUser {
   @Property
   private User user;

   void onActivate(@RequestParameter("userId") User user) {
      this.user = user;
   }
}

The @RequestParameter annotation may be placed on handler methods of both pages and components. The main use case for the annotations are probably methods handling ajax request.

 

  1. Alessio
  2. Igor Drobiazko
  3. Igor Drobiazko
  4. Serge

 

avatar

Your name

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

Tapestry 5 Blog - Copyright © 2009 - Eclectic Theme by Your Inspiration Web - Powered by WordPress