Tuesday, 8 September 2009

Leaf - Super Easy Paging for Tapestry


Leaf is a Tapestry 5 component that makes paging in tapestry applications super easy by inserting fully customisable paging links at the top and bottom of the rendered items. You can loop through the items on the page in the same way as the loop component.
<t:leaf.leaf source="source" value="item">
<h4>${item.title}</h4>
<p>${item.description}</p>
</t:leaf.leaf>

All the component requires is a PageSource implementation and a local value to populate the current paged item.
public class MyPage {

private static final int PAGE_AMOUNT = 10;

@Property
private PageItem item;
@Property
private PageSource source;

@Inject
private PageItemService pageItemService;

private int page;

void onActivate(int page) {
this.page = page;
}

void setupRender() throws IOException {

source = new PageSource(page, PAGE_AMOUNT) {

@Override
public Page loadPage(int start, int limit) {
return pageItemService.getPageItems(start, limit);
}
@Override
public Object[] getContext(int page) {
return new Object[]{page};
}
};
}
}

Customise the paging with the format parameter. This is a comma separated list of elements that determines the order and elements that get rendered. Two more parameters; top and bottom can also be provided to prevent the paging from getting displayed at either the top of bottom of the list.
<t:leaf.leaf source="source" value="item" 
format="textb,first,previous,pages,next,last" bottom="false">

To start using leaf paging add the norobot repository and leaf dependency to your pom.xml
<repository>
<id>tapestry</id>
<url>http://norobot.googlecode.com/svn/maven2</url>
</repository>
...
<dependency>
<groupId>org.norobot</groupId>
<artifactId>leaf</artifactId>
<version>1.1</version>
</dependency>

Sunday, 30 August 2009

Sessionless - Tapestry 5 without sessions

Sessionless is a tapestry plugin for webapps that don't want sessions. Tapestry uses the "redirect after post" pattern which results in book-markable pages and avoids duplicated form submissions at a small cost of storing some extra information in the session.

Sessionless removes the need to store form data in sessions by allowing pages to get rendered directly on unsuccessful form submissions. The redirect is maintained in all other (successful) posts. This means that form fields and error messages don't need to be persisted and a sessionless app can easily be built.

It works by decorating the RequestExceptionHandler with an implementation which catches PageValidationExceptions and renders a page directly. If no PageValidationException gets thrown then app will behave in the same way as a standard Tapestry application.

To use sessionless simply add the norobot repository to your pom
<repository>
<id>tapestry</id>
<url>http://norobot.googlecode.com/svn/maven2</url>
</repository>

Then add the dependency
<dependency>
<groupId>org.norobot</groupId>
<artifactId>sessionless</artifactId>
<version>1.0</version>
</dependency>

Then simply throw a PageValidationException in your submit method to get the page rendered without a redirect.
void onSubmitFromLogin() throws PageValidationException {
if (hasFormErrors()) {
throw new PageValidationException(this);
}
}

If the form has errors then the page gets rendered with no redirect.