How to set recursively component properties using Java Server Faces (JSF)

Hello,

I am using a piece of code that setting disabled=true and readonly=true to all components inside of a Form, but you can use this in other situations:

In your .jspx File:

<f:view beforePhase="#{someController.makeFormReadOnly}">

In your .java File:


import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import javax.faces.component.UIComponent;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import java.lang.reflect.Method;

public void makeFormReadOnly(FacesContext facesContext, UIComponent component) {
    try {
        Method[] methods = component.getClass().getMethods();
        for (Method method : methods) {
            if (method.getName().equals("getChildren")) {
                List<UIComponent> listaComp = (List<UIComponent>)method.invoke(component);
                for (UIComponent comp : listaComp) {
                    makeFormReadOnly(facesContext, comp);
                }
            }
        }
        component.setValueExpression("readOnly", getValueExpression(facesContext, "#{true}"));
        component.setValueExpression("disabled", getValueExpression(facesContext, "#{true}"));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

private ValueExpression getValueExpression(FacesContext facesContext, String name) {
    Application app = facesContext.getApplication();
    ExpressionFactory elFactory = app.getExpressionFactory();
    ELContext elContext = facesContext.getELContext();
    return elFactory.createValueExpression(elContext, name, Object.class);
}
Advertisements

11 thoughts on “How to set recursively component properties using Java Server Faces (JSF)

  1. Hi Victor,
    I have a form with different child components inside one of the is oracle.adf.view.rich.component.rich.layout.RichPanelStretchLayout type. And this component has different others child UIComponents inside it. Do you have any idea how can i recursively go into that panel? If i am starting from the form then RichPanelStretchLayout UIComponent gives me getChildCount() = 0.

    regards,
    Roby

  2. Hey again :-).

    i did some small amendment to your code so now it’s working also for PanelStretchLayout.

    public void makeFormReadOnly(FacesContext facesContext, UIComponent component) {
    try {
    Method[] methods = component.getClass().getMethods();
    for (Method method : methods) {
    if (method.getName().equals(“getFacetsAndChildren”)) {

    Iterator it = (Iterator)method.invoke(component);

    while (it.hasNext())
    {
    UIComponent comp = (UIComponent)it.next();
    makeFormReadOnly(facesContext, comp);
    }

    /*
    List listaComp = (List)method.invoke(component);
    for (UIComponent comp : listaComp) {
    makeFormReadOnly(facesContext, comp);
    }
    */
    }
    }
    component.setValueExpression(“readOnly”, getValueExpression(facesContext, “#{true}”));
    component.setValueExpression(“disabled”, getValueExpression(facesContext, “#{true}”));
    } catch (Exception e) {
    throw new RuntimeException(e);
    }
    }

    cheers,
    Roby

  3. Hi Victor,
    Great code. I only have one question. Could you let me know how the public method makeFormReadOnly will be triggered ?
    Thanks a lot in advance
    Dave

      1. hi victor
        I have requirement to make jsff page readonly onPageLoad based on some business logic flag…

        Which is the event in adf jsff page on which i can call this code/method?
        I hope there is no onPageLoad event in adf jsff page

  4. Sorry, didnt get you..

    Just curious to know : when should we call this method:

    public void makeFormReadOnly{…
    }

    be called ,

    in order to make ui components readonly onPageLoad

    1. Trying this..phaseListener option…
      Somewhere i have read in OTN that onPageLoad event can be implemented via methodActivity which is to be called before page call..

      Not sure how???

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