Follow by Email

Thursday, 20 August 2015

Set EL expression in value (properties) of programmatically created ADF Faces component


When we are working with ADF Faces components programmatically then we  create , set styles,set value of that component from managed bean
This post is about setting an expression as component value that will be resolved at run time and return desired value



Suppose i am creating an af:inputText and i want that it's value should be derived from an attribute that is present in page bindings. I have EmployeeId added in  binding section of pagedef, to get it's value through EL i have to use this expression- #{bindings.EmployeeId.inputValue}
Now i have to set this expression as value of programmatically created inputText
 
See this simple peace of code -




    /**Method to resolve a String to ValueExpression
     * @param data
     * @return
     */
    public ValueExpression getValueExpression(String data) {
        FacesContext fc = FacesContext.getCurrentInstance();
        Application app = fc.getApplication();
        ExpressionFactory elFactory = app.getExpressionFactory();
        ELContext elContext = fc.getELContext();
        ValueExpression valueExp = elFactory.createValueExpression(elContext, data, Object.class);
        return valueExp;

    }



    /**Method to add dynamically created component to a parent layout
     * @param parentUIComponent
     * @param childUIComponent
     */
    public void addComponent(UIComponent parentUIComponent, UIComponent childUIComponent) {
        parentUIComponent.getChildren().add(childUIComponent);
        AdfFacesContext.getCurrentInstance().addPartialTarget(parentUIComponent);
    }



    /**Method to create inputText programmatically and sets expression as it's value
     * @param actionEvent
     */
    public void createComp(ActionEvent actionEvent) {
        RichInputText ui = new RichInputText();

        //Resolve string to value expression
        ui.setValueExpression("value", getValueExpression("#{bindings.EmployeeId.inputValue}"));

        ui.setId("pit1");
        ui.setContentStyle("width:200px;color:navy");

        //getParentGroupLayoutBind is the component binding of parent panel group layout
        addComponent(getParentGroupLayoutBind(), ui);
    }


Cheers:) Happy Learning

1 comment :

  1. Hi!
    thank your code.
    but when I apply your code with richtextEdittor is error can you help me with richEdittor
    my code here:
    RichTextEditor ui = new RichTextEditor();

    ui.setId("ID"));

    ui.setContentStyle("font-weight:bold;color:red");

    ui.addValidator(Fn_validator("#{bean_control.getObject}"));
    ui.setValueExpression("binding", Fn_binding("#{bean_control.RTEValue"));
    addComponent(getPFL(), ui);
    Fn_binding here:
    public static ValueExpression Fn_binding(String binding_name){

    FacesContext fc = FacesContext.getCurrentInstance();
    Application app = fc.getApplication();
    ExpressionFactory elFactory = app.getExpressionFactory();
    ELContext elContext = fc.getELContext();
    ValueExpression valueExp = elFactory.createValueExpression(elContext, binding_name, Object.class);
    return valueExp;



    }
    can you help me how to get object value of RichTextEditor
    thank a lot!

    ReplyDelete