6

How do I bind a value of certain component dynamically at runtime? For example, I have the following component tag,

<h:inputText value="#{bean.someProp}" />

In my case, "#{bean.someProp}" is only known at runtime.

What's the best strategy to implement this?

Should I traverse the component tree and set the value binding programmatically? If yes, at which JSF lifecycle phase should I do the traversing?

bungrudi
  • 1,417
  • 1
  • 17
  • 24

2 Answers2

15

You can bind it to a Map<String, Object> bean property where the String key is less or more the dynamic property name. You can access map values in EL the following way:

<h:inputText value="#{bean.map.someProp}" />

or

<h:inputText value="#{bean.map['someProp']}" />

which can even be done a tad more dynamically where someVar actually resolves to a String value of "someProp":

<h:inputText value="#{bean.map[someVar]}" />

You only need to ensure that the Map is created during bean initialization, otherwise JSF can't access the map values. EL namely won't precreate "nested properties" for you. Thus, do e.g. direct instantiation:

public class Bean {
    private Map<String, Object> map = new HashMap<String, Object>();
}

.. or inside a Constructor or @PostConstruct if you like.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Ok I get what you meant. This is a good idea, except that I will loose all metadata defined in the annotations in my entities. This will also mean loosing the advantage of using (the hibernate validation tag from SEAM) and all other infrastructure that depends on annotation metadata. FYI, I'm doing this to make a framework that implements 'naked object' pattern (one ref implementation is http://www.nakedobjects.org/). Basically it inspect an entity and automatically build related tables and forms (in this case, component tree). – bungrudi Feb 09 '10 at 02:08
  • Urm,... on the second thought, using this approach I don't have to loose all that annotations metadata. I'll try this and let you know. – bungrudi Feb 09 '10 at 03:32
  • This is the way i do it currently. By doing this, I'm loosing the advantage of and has to re-implement validation somewhere else. It's trivial though, just a matter of calling hibernateValidator.validate(bean). – bungrudi Sep 30 '10 at 01:00
  • Does this imply that it is impossible to do a read/write binding to a bean property (with setter/getter) with the `#{bean.map['someProp']}`? This would be surprising because it is possible to read a setter/getter property with that syntax. – Lii Oct 13 '14 at 16:36
0

Another option is you can add a layer of abstraction to your bean.

public String getDynamicProp() {
   ...Code to determine and return the correct property based on the meta-data...
}

public void setDynamicProp(String input) {
   ...Code to determine and return the correct property based on the meta-data...
}

Then you would tie your JSF directly to the dynamic prop:

#{bean.dynamicProp}

However, this won't hang on to the metadata like validators and the like you were wanting. However, you can programatically invoke validation and handle it yourself.

Drew
  • 15,158
  • 17
  • 68
  • 77