21 July 2009

Referencing an object in JSF/Richfaces

With JSF 1.2, referencing objects in the component tree isn't as straightforward as it used to be. The findComponent() method has been deprecated, so we must use other means to find an object. Jacob Hookom and Ric Smith provide slight variations on how to accomplish this task. Both involve using the ContextCallback object; here's a snippet to give you an idea:
public static final ContextCallback RENDER = new ContextCallback() {
public void invokeContextCallback(FacesContext ctx, UIComponent c) {

boolean found = root.invokeOnComponent(faces, clientId, RENDER);

if (!found) throw new FacesException(clientId + " not found!");

Note in the above code, you use the invokeOnComponent() method to (what else?) invoke a particular action on the component in question. Note that you need to use the client ID for the object, which in JSF looks something like this: myForm:myComponentID

In turn, the RENDER method calls invokeContextCallback(), which carries out some action on the component. This can be handy for programmatically adding controls to the page at a specific location; c.renderAll(ctx) can be replaced with whatever you need.

You can also add objects to the page using Max Katz' technique, though I'm not sure if that way you're allowed to put them at a specific location in the component tree.

No comments: