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?
You can bind it to a
Map<String, Object>bean property where theStringkey is less or more the dynamic property name. You can access map values in EL the following way:or
which can even be done a tad more dynamically where
someVaractually resolves to aStringvalue of"someProp":You only need to ensure that the
Mapis 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:.. or inside a Constructor or
@PostConstructif you like.