Thursday, June 14, 2007

JSF Portlet Fun: Request Scoped Beans Don't Survive From Action to Render Phase

Not sure if this is Pluto specific, but request scoped beans basically don't work as expected since they get initialized on the action phase and then again on the render phase. This is with MyFaces 1.1.4 and Pluto 1.1.3. At least with GridSphere 2.1.5 and MyFaces 1.0.9, I know that request scoped beans do work. See for more information. The workaround is basic and actually standard from the Portlet API point of view. The idea is that for any thing that you want to carry over from the action phase to the render phase, then just add this as a render parameter. In this example, a field of the request scoped bean called "success" is a boolean. So in the action method, I do this:

// add the success field to a render parameter so it will be available in
// the render phase. See constructor as well.
ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
if (extContext.getResponse() instanceof ActionResponse) {
ActionResponse actionResponse = (ActionResponse) extContext.getResponse();
actionResponse.setRenderParameter("success", Boolean.toString(success));

Then in the contructor for the bean I have:
// Pull success value from render parameter set in action phase, i.e., submit()
ExternalContext extContext = facesContext.getExternalContext();
Map params = extContext.getRequestParameterMap();

if (params.containsKey("success")) {
this.success = Boolean.parseBoolean((String)params.get("success"));

1 comment:

ming said...

Hi, just wanted to say I found your post very useful :)
We were having major issues trying to figure out why the pagecode was no longer being retained in RAD 7, but seemed to work fine in RAD 6.
At least we now have a solution.