Skip to content

Server Side Include with Japid

branaway edited this page Sep 14, 2010 · 8 revisions

A Japid page can be broken into multiple tiles and each tile is backed by an action, remotely similar to the portlet concept, or servicer-side-include.

More interesting is that each action invocation can take a different cache control, so that a complex page can be assembled with dynamic html tiles that have different timing requirement.

This mechanism can be used for example to construct a home page where all the content can be cached for 1 hour whereas the comment area must be no older than 1 minute.

`args models.japidsample.Post post 
`import controllers.japid.SampleController
 
<p>This is the composite content header</p>
   
<div>#{invoke SampleController.authorPanel(post.getAuthor())/}</div>
 
<div>this one has full cache control</div>

<div>#{invoke SampleController.authorPanel(post.getAuthor()), "10s", post.getAuthor()/}</div>
 
<div>this one has cache control using the default signature. </div>

<div>#{invoke SampleController.authorPanel(post.getAuthor()), "10s"/}</div>

There are three forms of using the invoke tag:

Form 1: simple invocation without caching

#{invoke SampleController.authorPanel(post.getAuthor())/}

The tag invokes the authorPanel() method of SampleController, with a parameter post.getAuthor().

Form 2: invocation with default cache key

#{invoke SampleController.authorPanel(), "10s"/}

The above code invoke the target action and cache the result for 10 seconds under the key of “SampleController.authorPanel”.

Form 3: invocation with specific key

#{invoke SampleController.authorPanel(post.getAuthor()), "10s", post.getAuthor()/}

The above code invoke the target action and cache the result for 10 seconds under the key of “SampleController.authorPanel” + post.getAuthor().

Note about the parameters:

  1. The first parameter is the action invocation expression with the proper arguments to the action. The action result will not be cached if there is only one parameter. The controller must with be directly in the controllers package, or they need to be fully qualified, or imported in the header.
  2. The second parameter, if present, is the Time-To-Alive for the result to be cached for, in the format of “10s” for 10 seconds, “10mn” for 10 minutes, “1h” for one hour, for example.
  3. The third and more parameters, if present, will be used to compose a composite key to cache the result for.

To make a key unique, developers need to make sure the toString() method will return a unique value to identify an object. For example: a JPA model will usually embed the ID field in the toString() so it will be unique.

The cache internally use the Cache object provided Play! framework.

How is this different from simple include and tag invocation

An invoke is a “direct” invocation of a Play action, with its data processing logic and page rendering of complexity of any level. The Play’s include mechanism is a simple text replacement before rendering. Regular Tag invocation is more sophisticated but they usually rely on the caller to pass in data to render. In comparison, the invoke tag offers really powerful way of page composition:

  1. each composite action are independent of each other, thus can be independently tested
  2. the render result from each action call can be cached with a expiration time just for this part of the content.
  3. it’s fast because it directly calls the action without going through the full stack.

For any serious complex pages, the japid #{invoke} tag is the way to go.