Monday, June 29, 2015

How to set common styleClass when component is required, readonly or disabled

This is to show how to highlight input components globally when it is either required, readonly, or disabled.

1. Create a class that is extending the javax.faces.application.ViewHandlerWrapper.
public class MyViewHandlerWrapper extends ViewHandlerWrapper {

}

2. Overrider the renderView method.
@Override
public void renderView(FacesContext fc, UIViewRoot view) throws IOException, FacesException {

  super.renderView(fc, view);

}

3. create a private method to find input components recursively and highlight them based on the required, readonly and disabled attribute.

private void highlightInputRecursively(List<UIComponent> childrenList) {

  for(UIComponent component : childrenList) {
   if(component.getChildCount() > 0) {
    findInputRecursively(component.getChildren());
   } else {
    if(component instanceof UIInput) {
     if(((UIInput) component).isRequired()) {
      component.getAttributes().put("styleClass", "ui-input-required");

     }
    }
   }
  }
 }

4. call the private method before the line super.renderView(fc, view);
public void renderView(FacesContext fc, UIViewRoot view) throws IOException, FacesException {

    highlightInputRecursively(view.getChildren());

    super.renderView(fc, view);

}

5. last, configure the new ViewHandlerWrapper in the faces-config.xml
<application>

    <view-handler>Your.Package.ViewHandlerWrapper</view-handler>

</application>


Done!!

LinkWithin

Related Posts Plugin for WordPress, Blogger...