Create components in runtime

Sometimes, the case, that can occur we want to create different components according to the state from the page or some action that has been executed in the same one. A solution to show the wished component would be to create a switcher component and be rotating the visibility of the component wished at every moment, but this can be a technique limited enough and “sometimes troublesome” to write the file jspx of our page.

A quite useful possibility is the one to create the component in run time, thus we would save extra code in the file jspx and in addition it gives control much more us of which we want to show in the page. With this we would clear an unnecessary weight to the initial charge of the page, because we will only load the components when we need them.

The way to create a component would be thus:

    FacesContext.getCurrentInstance().getApplication().
       createComponent(COMPONENTE.COMPONENT_TYPE);
This call would give like result a Object that we must cast to the wished component:

    CoreInputText input = (CoreInputText)FacesContext.
        getCurrentInstance().getApplication().
        createComponent(CoreInputText.COMPONENT_TYPE);

Once we have created the component, only we would already have left to give him the typical values of similar way to since we do it in jspx:
    input.setId("id1");
    input.setLabel("Etiqueta");

In the same way that we can create components, we can also create validators or converters in run time, the only thing that it would change is that in time to give as parameter constant COMPONENT_TYPE we would pass another constant called COMPONENT_FAMILY:

    FacesContext.getCurrentInstance().getApplication().
       createComponent(Validator.COMPONENT_FAMILY);


There will be times that besides to create the component, we want to associate actions to him to he himself, and lack makes associate to action a MethodBinding to him, the way to create it is not very complicated, simply we must know as it is the name associated to backing_bean and that values is going to receive like parameter the method to which we are going it to associate. The method to make is using it the following method:
    FacesContext.getCurrentInstance().getApplication().
       createMethodBinding(String, Class[]);

Where string that receives corresponds to label EL that reference to method that we want to call, that is to say, “# {nombre_backing_bean.metodo}”, and as second parameter receives an Array of the classes that is going to receive like parameters, like for example:

    Class[] array = new Class[1];
    array[0] = ValueChangeEvent.class;

This MethodBinding that we will receive we have it to associate to the component, by means of its corresponding one setter.

When we created components, although it is not required, always is advisable to set an id, there are a method which it is possible to be acceded to him through FacesContext, that will generá to us you go only for that component in the page, but in pages where we use to the same time several east UIViewRoot objects method can cause many problems to us, more than to help us. This happens because that method simply crosses the list of existing components for that object viewRoot and it assigns a found value to him corelative to the last one. Therefore, the best thing always will be to assign to him you go of a safe and unique way for all the cases that can be given in the page. The method is the following one:

    FacesContext.getCurrentInstance().getViewRoot().
       createUniqueId();

Once we already know like creating components, validators, etc only we have left to show them in some place of the page, because we have them created, but not positioned. The way to insert it in some place of the page is very simple, it would be acceding to the method getChildren () of some component that allows to insert children to him, and to add our component. The form to do it is always the same one:

    getPanel().getChildren().add(input);

Tags:

Comments are closed.