Friday, February 12, 2010

RESTful GET URLs with JSF 2.0

JSF 2.0 introduces Bookmarkable URLs with so-called "view params", when combined with the new h:link component, can be used to implement RESTful GET URLs.

Traditionally JSF is using POST exclusively. It was possible to use GET but in practice this required some workarounds, especially because "page load action" is not natively supported by JSF. In effect, making Ruby on Rails-style controller ridiculously hard to implement.

JSF 2.0 improves this. Let's see how it's done.

A JSF-style show entity link looks like this:
<h:commandLink action="#{campaignController.prepareView}"
Where CampaignController.prepareView() essentially sets the current entity bean that will be displayed by the next view:
public String prepareView() {
    current = (Campaign)getItems().getRowData();
    selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
    return "View";
This works, but it's quite overkill to use HTTP POST just for showing a page of entity info. To change it to GET, we use h:link :
<h:link outcome="ViewOne" value="Show Campaign">
  <f:param name="campaignId" value="#{}" />
This will generate a URL like http://localhost:8080/app/faces/campaign/View?campaignId=7

We'll need to process this new parameter "campaignId", by declaring a viewParam inside f:metadata in the destination JSF view (ViewOne.xhtml):
  <f:viewParam name="campaignId" value="#{campaignController.campaignId}"/>
The campaignController.campaignId refers to a property, therefore we create the setter and getter in the controller class:
public Long getCampaignId() {
    if (current != null) return current.getId();
    else return null;

public void setCampaignId(Long campaignId) {
    current = ejbFacade.find(campaignId);
That's it!

The URL is still not pretty / SEO-friendly, but for that purpose we can use PrettyFaces.

Related resources:


polo outlet said... Burberry Outlet Air Max Shoes Coach Black Friday Coach Black Friday Coach Cyber Monday Coach Factory Coach Outlet Factory Coach Outlet USA Coach Purses Factory Coach Purses USA Coach Store Outlet Coach Purses On Sale Monster Beats Outlet Louis Vuitton Outlet Louis Vuitton Factory Marc Jacobs On Sale MCM Outlet MCM 店铺 cheap oakley sunglasses Michael Kors Outlet Michael Kors Outlet Michael Kors Factory Michael Kors Outlet Michael Kors Factory Online Coach Factory Outlet North Face Outlet Online Polo Outlet Store Ralph Lauren UK Sac Longchamp Pairs Coach Outlet Online Coach Factory Oultet Barbour Jackets Outlet Online Canada Goose Outlet Gucci Outlet Online Michael Kors Outlet Moncler Clearance Moncler Jackets Outlet Online North Clearace Outlet Polo Ralph Lauren Outlet Online Woolrich Clearance Michael Kors Factory Outlet Coach Outlet Online USA Beats by Dre Coach Purses Outlet Online Michael Kors USA

Nithyanantham M said...

Thanks for sharing this post
Dailymotion video downloader

Try Dailymotion video
to download videos from dailymotion.

Post a Comment

Copyright 2009 Spring vs Java EE Web Dev. Powered by Blogger Blogger Templates create by Deluxe Templates. WP by Masterplan