Force a new instance of a bean in @ViewAccessScope

Goal

Force a new JSF request to a page containing a bean in @ViewAccessScoped scope

Description

This recipe shows a simple way to force a request to a page where a backing bean in @ViewAccessScope is already constructed, i.e., if you are in a page x_detail.xhtml backed up with a managed bean XBean at @ViewAccessScope and you want to go to the same page x_detail.xhtml with a new instance of XBean, for instance, because in the first case you are at edit mode of a given x data and on the second case, you want to create a new instance of x.

How to

Maybe there are simpler ways to do that, but this recipe exposes a simple trick, consisting on defining a param windowId with an empty value to expose the action link to the page. For instance:

<h:commandLink value="Create" action="#{xBean.prepareCreate}">
  <f:param name="windowId" value="" />
</h:commandLink>

Explanations

Since @ViewAccessScope beans are maintained in scope according to the windowId parameter, if you set the windowId param with an empty value, the request to the same page will be a new request because the bean won’t be associated to that windowId, thus leading to the creation of a new backing bean instance.

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