Friday, January 30, 2015

ADF Component id and clientId

When we drag a ADF component to a page, ADF will generate an id like below:

<af:inputText id="it1" />

The id attribute rendered in the browser is obviously not the same as the value set in the ADF apge. This new id is the clientId and is produced by the UIComponent.getClientId(FacesContext) method.

public String getClientId(String componentId) {
    FacesContext context = FacesContext.getCurrentInstance();
    UIViewRoot root = context.getViewRoot();    
    UIComponent c = findComponent(root, componentId);
    return c.getClientId(context);
}

One good example of using client id is to display error message on a specific field.

The field will has a red border and mouse over to see the error message.
    
public static void displayMessage(Severity messageType,
                                      String messageTitle, String messageText,
                                      String componentId) {
        String clientId = null;
        if (componentId != null) {
            clientId = getClientId(componentId);
        }
        FacesContext fc = FacesContext.getCurrentInstance();
        FacesMessage message = new FacesMessage(messageTitle, messageText);
        message.setSeverity(messageType);
        fc.addMessage(clientId, message);

}




No comments:

Post a Comment