3

I implement a simple method, that iterate JSF components tree and sets the components to disabled. (So the user cannot change the values). But this method doesn't function for composite components. How can I detect a composite component at least? Then I can try to set the special attribute to disabled.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Tony
  • 2,266
  • 4
  • 33
  • 54
  • You can't detect whether something is a composite, since the only limitation on a composite component backing bean type is implementing `NamingContainer`, but there are plenty of other things implementing that interface. – rdcrng Aug 16 '13 at 09:30
  • Please ask one question per question. Do not chameleonize existing questions. It makes answers incomplete or even invalid. – BalusC Aug 16 '13 at 12:49
  • @BalusC Sorry, but this was my first question and the idea about security comes later when I thought about this. Could you recreate your answer? Your answer was very useful. – Tony Aug 16 '13 at 13:06

1 Answers1

4

The UIComponent class has a isCompositeComponent() helper method for exactly this purpose.

So, this should just do:

for (UIComponent child : component.getChildren()) {
    if (UIComponent.isCompositeComponent(child)) {
        // It's a composite child!
    }
}

For the interested in "under the covers" workings, here's the implementation source code from Mojarra 2.1.25:

public static boolean isCompositeComponent(UIComponent component) {

    if (component == null) {
        throw new NullPointerException();
    }
    boolean result = false;
    if (null != component.isCompositeComponent) {
        result = component.isCompositeComponent.booleanValue();
    } else {
        result = component.isCompositeComponent =
                (component.getAttributes().containsKey(
                           Resource.COMPONENT_RESOURCE_KEY));
    }
    return result;

}

It's thus identified by the presence of a component attribute with the name as definied by Resource.COMPONENT_RESOURCE_KEY which has the value of "javax.faces.application.Resource.ComponentResource".

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you for your help! But now I am thinking about security issues. I have edited my question. – Tony Aug 16 '13 at 12:40
  • The link to my security question [Security issues JSF. Disabled attribute on server side](http://stackoverflow.com/questions/18274010/security-issues-jsf-disabled-attribute-on-server-side) – Tony Aug 16 '13 at 13:16