tags:

views:

25

answers:

1

Hi all,

Imagine a text box, bound to a ManagedBean:

<h:inputText id="name" value="#{mb.name}"/>

I would like to default the field to a value.

I know I can set the value in the ManagedBean at construction time, but that doesn't work for me as I use the same ManagedBean as a backing bean in different xhtml pages. And I don't want the 'name' field to be initialized in all those pages.

Can somebody suggest a strategy? Or am I missing something essential?

Thank you very much! J.

+1  A: 
  • you can fill the value in with javascript (document.getElementById("formName:textName").value = 'defaultValue';. Perhaps on document.onload

  • you can subclass your managed bean and fill the default value in the default constructor, or in a @PostConstruct method, and use the subclassed bean.

Bozho
Thx Bozho. The JavaScript answer does the trick!I don't think that the alternative (@PostConstruct) works for me. I'm using viewparams and because of the name matching involved, the bean name in from-view needs to equal the bean name in the to-view. However, do you think I could detect the name of the calling *.xhtml page by inspecting some context object? I explored FacesContext using the debugger, but I couldn't find a typesafe way to get the from-view from the Request object. Anyway, this approach doesn't look as clean as the JavaScript one. Just curious. ;-)Thx. J.
Jan
You can, using `getViewRoot().getViewId()`. Btw, if an answer works for you - mark it as accepted (tick below the vote counter)
Bozho