Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting default value to h: selectOneMenu on button click

 
Ziaur Rahman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am displaying a output panel on button click, it has an selectonemenu item, by default value 1 is selected, when i change some value and click CANCEL button and reopen output panel, the last selected value is getting displayed. is there any way to display default value "1"... thanks in advance



<a4jutputPanel id="new">

<h:selectOneMenu id="menuID" value="#{myController.count}" immediate="true">
<f:selectItem itemLabel="1" itemValue="1" />
<f:selectItem itemLabel="2" itemValue="2" />
<f:selectItem itemLabel="3" itemValue="3" />
<f:selectItem itemLabel="4" itemValue="4" />
<f:selectItem itemLabel="5" itemValue="5" />
<f:selectItem itemLabel="6" itemValue="6" />
<f:selectItem itemLabel="7" itemValue="7" />
<f:selectItem itemLabel="8" itemValue="8" />
<f:selectItem itemLabel="9" itemValue="9" />
<f:selectItem itemLabel="10" itemValue="10" />
</h:selectOneMenu></td>

<div id="popActionButtons">
<h:commandLink value="cancel" onclick="return closePoUpWindow(this);" />

<c1:ajaxCommandButton id="save" title="Save"
onclick="return vailidatesav();"
value="save" validationType="required"
eventType="click" styleClass="inputButton" /></div>
</div>
</a4jutputPanel>
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18012
47
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Ziaur!

You can make the XML more readable by using the "Code" button on our message editor.

When a JSF page is displayed or refreshed, the values for the controls in its forms are set to match the values referenced in the backing beans. So to set the default value to "1", set myController.count to 1 before displaying the page.

Aside from that, I should note that I'm pretty sure that "immediate=true" is not a valid attribute for selectOneMenu and that backing beans are Models, not Controllers, so technically "myController" as a bean name is incorrect.

In JSF, your Controllers are part of JSF itself. You only have to create the Model objects and View templates.
 
Ziaur Rahman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Tim,
But how do I set myController.count to 1 everytime when I load the page?
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18012
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever a page is rendered, the current values of the backing bean (Model) properties are used to set the displayed control values. So to pre-set the control value, set the backing bean property value.

Note that since JSF operates according to Model/View/Controller rules, a page may be re-displayed multiple times, allowing the user to see validation error messages and correct faulty input data.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic