UIComponent references in session scoped JSF beans

In this blog my colleague Blake Sullivan described a lot of issues with JSF managed beans that are placed in the session. This maybe easy for the developer on first hand, but the tax you pay later..

Even if you do take and implement (correctly) the Serializable interface, the oddness starts when you have UIComponent fields inside of those beans (mostly the case IF the developer uses the binding attribute). But hey, it’s good that the UIComponent don’t implement Serializable…

In Trinidad we offer some help for that (if you have to (or need to) use UIComponents in those “session beans”) with the ComponentReference util. Inside of your session scoped managed bean you don’t use the components directly, you use the tool. Here is a code snippet for a “UIComponent field” and its set/get:

...
private ComponentReference<UIInput> searchField;
...
public void setSearchField(UIInput searchField)
{
 if( this.searchField == null)
 this.searchField = ComponentReference.newUIComponentReference(searchField);
}

public UIInput getSearchField()
{
 return searchField ==null ? null : searchField.getComponent();
}
....

So, when you need a reference of the UIComponent in question, call the “getComponent()”. When the reference object is created it remembers the entire path to the UIComponent (by remembering all index or the names of facets). The getComponent() walks down the remembered path to look it up from there. If we hadn’t luck with the path, the reference util uses a scoped ID and calls the regular “findComponent()“. If that was successful, we renew the path and return the desired UIComponent instance. However, a few things to remember when using the util:

  • This class is not thread-safe, since it depends on UIComponent APIs
  • The passed in UIComponent is required to have an ID
  • The reference will break if the UIComponent is moved between NamingContainers or if any of the ancestor NamingContainers have their IDs changed
  • The refrence is persistable. However UIComponents are not Serializable and therefore can not be used at any scope longer than request

We have a good amount of different JUnit test-cases, including simulating some failover – this will give you also some ideas on how to use the util.

So, now please no longer use session-based managed beans WITH UIComponents as their members ;-)

About these ads

Howdy!

Posted in apache, java, jsf, myfaces, trinidad
One comment on “UIComponent references in session scoped JSF beans

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 32 other followers

%d bloggers like this: