Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

component binding vs findComponent() - when to use which?

Tags:

jsf

As described in this question I try to perform some field validation in a form on the backing bean side. For this I would like to access the violating fields to mark them. From searching the web there seem to be two ways to do this:

  • store the components in the backing bean for access and use them in the JSF pages via the binding attribute.
  • Use standard value binding in the JSF pages and when needing access to a component from the bean, look it up via UIViewRoot.findComponent(String id)

As far as I can see both ways have drawbacks: Component bindings blows up the backing bean with variables and getters/setters, some sites strongly discourage the use of component binding at all. In any case, a request scope is advised. On the other hand, findComponent() always traverses the tree, which may or may not be costly, right? (Plus, at the moment I can't find my component at all, but that is another problem)

Which would be the way to go? Are these interchangeable alternatives and if not, based on what criteria do you chose? Currently I just don't have enough insight to make a decent decision...

like image 247
Louise Avatar asked Sep 18 '12 09:09

Louise


1 Answers

First of all, regardless of the choice, both are a poor practice. See also How does the 'binding' attribute work in JSF? When and how should it be used?

If you had to make the choice, component bindings are definitely faster and cheaper. It makes logically completely sense that a tree scan as done by UIComponent#findComponent() has its performance implications.

Indeed, the backing bean holding the component bindings must be request scoped, but you could easily inject a different scoped backing bean holding the business logic in it by @ManagedProperty.

A cleaner approach would be to use a Map as holder of all component bindings. You only need to add the following entry to faces-config.xml:

<managed-bean>
    <managed-bean-name>components</managed-bean-name>
    <managed-bean-class>java.util.HashMap</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

This can just be used as

<h:inputSome binding="#{components.input1}" />
<h:inputSome binding="#{components.input2}" />
<h:inputSome binding="#{components.input3}" />

And this can be obtained in other beans as

Map<String, UIComponent> components = (Map<String, UIComponent>) externalContext.getRequestMap().get("components");

This way you don't need to worry about specifying individual properties/getters/setters. In the above example, the Map will contain three entries with keys input1, input2 and input3, each with the respective UIComponent instance as value.


Unrelated to the concrete question, there may be a much simpler solution to the concrete problem as you described in the other question than performing the validation in the action method (which is actually Bad Design). I've posted an answer over there.

like image 118
BalusC Avatar answered Nov 15 '22 09:11

BalusC