1

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
  • Using JSF Converter to convert the value from long, int, double ...etc to Wrapper Data types Long, Integer Commented Apr 21, 2013 at 6:09

1 Answer 1

8
  • 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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
You can, using getViewRoot().getViewId(). Btw, if an answer works for you - mark it as accepted (tick below the vote counter)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.