Bookmarkable Pages with JSF

In many applications it is a requirement to link to pages that accept arguments via HTTP-GET parameters. Over the last few months I've been working with Seam quite a bit, where it is pretty easy to create bookmarkable pages. In fact, this use case is so common that it is discussed in the introductory chapter of Seam's documentation.

In plain JSF, you don't have the fancy features pages.xml provides. The solution lies in the managed bean facility. You configure a managed property which receives a parameter from the request:

<managed-bean>
  <managed-bean-name>entityView</managed-bean-name>
  <managed-bean-class>EntityView</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
  <managed-property>
    <property-name>id</property-name>
    <property-class>java.lang.Integer</property-class>
    <value>#{param.id}</value>
  </managed-property>
</managed-bean>

In this example, an id request parameter is mapped to EntityView's id property. The EntityView class looks something like this:

class EntityView {
  private Integer id;

  public Integer getId() {
    return this.id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public Entity getEntity() {
    // use the id attribute to load the entity
  }
}

The getEntity() retrieves the Entity object (probably from a database) and returns it to the calling JSP or Facelets page (#{entityView.entity}). The EntityView can also be used to provide wrapper code for display purposes, like turning Lists into ListDataModels.

Note that getEntity() may be called multiple times during the JSF lifecycle, so it makes sense to cache the Entity object in the EntityView. Also make sure to provide an exception handling page in case the passed in parameter is no integer.

social