Tuesday, October 28, 2014

Best technique to bind the UI Components to the managed bean in JSF/ADF

Problem

Runtime exceptions saying can't serialize rich ui component class.

Why is it occurring?

Binding a JSF UI Component to a managed bean which is in a scope greater than requestScope or backingBeanScope. We should not do that because the UI components are not serializable and the application might not work properly in the clustered environment. We should bind the JSF components to the managed beans which are either in requestScope or backingBeanScope.

What is so special about requestScope and backingBeanScope?

For backingBeanScope or requestScope bean the class references need not be serialized. So inorder to avoid any serialization errors it is recommended to keep the UI component references in a backing bean scoped or request scope bean so as to avoid any serialization done on these class references.

But requestScope or backingBeanScope is not sufficient for my scenario, what to do?

  1. Configure the manged bean (MB1) to the taskflow in the backingBeanScope
  2. Configure another managed bean (MB2) in the required scope, pageFlowScope or viewScope
  3. Refer the UI Components of MB1 in MB2.
Example

BackingBean.java  (BackingBean scope/ Request scope)

import oracle.adf.view.rich.component.rich.layout.RichInputText;
public RichInputText getRichInputText(){
 // implement the getter method as needed
}

// Access these getters on your pageFlowScope Bean
PageFlowScopeBean.java implements Serialization (PageFlowScope)

import javax.faces.context.FacesContext;
public void uiCompRefFromBackingBean(){
    // get the backingbean instance   
    FacesContext facesContext = FacesContext.getCurrentInstance();
    BackingBean backingBean = (BackingBean)facesContext.getExternalContext().getRequestMap().get("BackingBean");
   
    // get the component instance
    RichInputText tree = backingBean.getRichInputText();
 }




No comments:

Post a Comment