This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSF and the fly likes Setting default value to h: selectOneMenu on button click Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Setting default value to h: selectOneMenu on button click" Watch "Setting default value to h: selectOneMenu on button click" New topic
Author

Setting default value to h: selectOneMenu on button click

Ziaur Rahman
Greenhorn

Joined: Feb 08, 2011
Posts: 4
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

Joined: Jun 25, 2001
Posts: 15950
    
  19

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
Ziaur Rahman
Greenhorn

Joined: Feb 08, 2011
Posts: 4
Thanks for your reply Tim,
But how do I set myController.count to 1 everytime when I load the page?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15950
    
  19

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Setting default value to h: selectOneMenu on button click
 
Similar Threads
Updating a form in primefaces when a selection is made in the datatable
valuechangelistener - getoldvalue() does not work fine, always returns the same
JSF - Missing source code using templating and composition
Validation Error on SelectOneMenu
multiple form in xhtml page nad update different controlls on two different forms