JSF SelectOneMenu Value not passing values to actionbean - jsf

I have a set of selectoneMenu list. The behavior of the dropdown is not constant.
Sometimes it passes the value as null, or still holds the existing values.
Anything i missed out? Or need to change any configuration?
<h:selectOneMenu id="blSearchzoneInput" value="#{bLDashBoardAction.zoneId}">
<f:selectItems value="#{bLDashBoardAction.zoneList}" var="c"
itemValue="#{c.zoneId}" />
</h:selectOneMenu>
thanks in advance

<h:selectOneMenu id="blSearchzoneInput" value="#{bLDashBoardAction.zoneId}">
<f:selectItems value="#{bLDashBoardAction.zoneList}" var="c"
itemValue="#{c.zoneId}" immediate="true" onchange="javascript: return this.form.submit();" />
</h:selectOneMenu>

Related

selectOneMenu calls value setter twice

I have a selectOneMenu and the issue with it is that it updates the value twice!! first time with the value I chose, immediately after that it updates the value with the number it previously had!
<p:selectOneMenu id="qupdate" value="#{object.pquantity}">
<f:selectItem styleClass="form-control"
itemLabel="-- SELECT QUANTITY -- " itemValue=""
noSelectionOption="true" />
<f:selectItems value="#{selectonemenu.quantoptions}" var="f"
itemLabel="#{f}" itemValue="#{f}" />
<p:ajax execute="qupdate" event="change"
listener="#{Bean.quantitychange(object.pquantity, object.id)}" />
</p:selectOneMenu>
is there anyway to have this working, I have tried to trace and it actually calls the setter twice!
#Holger is correct in the comments. p:selectOneMenu does not require a listener to actually process the value change. One should use a listener only to actually make other kind of interactions in the bean.
In this case, the fixed code should look like this:
<p:selectOneMenu id="qupdate" value="#{object.pquantity}">
<f:selectItem styleClass="form-control"
itemLabel="-- SELECT QUANTITY -- "
itemValue=""
noSelectionOption="true" />
<f:selectItems value="#{selectonemenu.quantoptions}"
var="f"
itemLabel="#{f}"
itemValue="#{f}" />
</p:selectOneMenu>
For more information about how the ajax listener is supposed to be used, please refer to to this question.

Default value from a list not passed to the next page while using <p:selectOneMenu> in jsf datatable

Default value from a list not passed to the next page while using in jsf datatable,all the default values get replaced with null value for all the items not selected in the datatable
You need to save the list using the managedbean in session scope.
Heading ##
<!-- <h:outputText value="#{rule.userAlertCategory.label}"/> -->
<p:selectOneMenu id="catCombo" value="#{rule.userAlertCategory}" style = "width:100%" converter="srUserAlertCategorySelectItemsConverter">
<p:ajax event="change" update="subCatCombo" listener="#{newAlertSetupController.onCategoryChanged}" process="#this"/>
<f:selectItem itemLabel="Select One" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{newAlertSetupController.userAlertCategoryList}" var="cat" itemLabel="#{cat.label}" itemValue="#{cat}"/>
</p:selectOneMenu>
</p:column>

Rerender multiple h:panelGrid on change of h:selectOneMenu in JSF 1.2

The situation:
I want to display specific panelGrid(s) on the page based on drop-down selection. I am doing this by storing several boolean which get set to true/false based on which item is selected in the drop down. I use the onchange="submit()" to refresh/re-render the page.
The problem:
I have validation on many fields in the form, so if I select an item it does validation and will not display the form.
The question:
How do I get the selectOneMenu change to re-render only the set of controls on the page being impacted instead of the whole page and causing validation?
The code: (JSF 1.2)
<h:selectOneMenu id="list" value="#{sessionBean.myDecorator.selectedItem}"
onchange="submit()" immediate="true" required="true"
valueChangeListener="#{requestBean.listChanged}">
<f:selectItem itemValue="#{null}" itemLabel="--Select One--" noSelectionOption="true" />
<f:selectItems value="#{sessionBean.myDecorator.mapItems}" />
</h:selectOneMenu>
Field causing validation:
<h:inputTextarea id="lblRequiredField" cols="100" rows="3" required="true" immediate="true"
value="#{sessionBean.myDecorator.myDo.myField}">
<f:validateLength minimum="1" maximum="300" />
</h:inputTextarea>

set default value for selectonemenu

I have a select menu like this :
<h:selectOneMenu value="#{serverMB.selectedServerType}" >
<f:converter binding="#{serverTypeConverter}"/>
<f:selectItems value="#{serverMB.serverTypesList}" var="servertypes"
itemLabel="#{servertypes.server_type_name}" />
<f:ajax listener="#{serverMB.changeSelectedOneMenuServerType}" render="selectservertype"/>
</h:selectOneMenu>
I want the default value of the select menu to be the value of one object that has been filled before, something like this:
<h:selectOneMenu value="#{serverMB.selectedServerType}" default="serverMB.server.servertype"> // <- default
or like this:
<h:selectOneMenu value="#{serverMB.selectedServerType}">
<f:converter binding="#{serverTypeConverter}"/>
<f:selectItem value"serverMB.server.servertype"/> <- but this item was the first one and has been selected
<f:selectItems value="#{serverMB.serverTypesList}" var="servertypes"
itemLabel="#{servertypes.server_type_name}" />
</h:selectOneMenu>
You can do something like this if your object is already populated when you call the page, if it is not, it will show the <f:selectItem/> message:
<h:selectOneMenu value="#{serverMB.server.servertype}">
<f:converter binding="#{serverTypeConverter}"/>
<f:selectItem itemLabel="Select a Server..." />
<f:selectItems value="#{serverMB.serverTypesList}" var="servertypes"
itemLabel="#{servertypes.server_type_name}"/>
<f:ajax listener="#{serverMB.changeSelectedOneMenuServerType}"
render="selectservertype"/>
</h:selectOneMenu>
You can also use the OmniFaces converter.
Here is my example. It works fine for me. (Note: Year is a object. Make sure, you have equals implemented. Year is name and id only.)
<h:selectOneMenu value="#{bean.user.year}"
converter="omnifaces.SelectItemsConverter">
<f:selectItem itemLabel="Please select a year." itemValue="#{null}"
noSelectionOption="true" />
<f:selectItems value="#{bean.allyears}" var="year"
itemValue="#{year}" itemLabel="#{year.name}" />
</h:selectOneMenu>

changing values of selectOneMenu in JSF

I am new to JSF and i am stuck at one place. I have one dropdown list which contains two items.
Bank
Cash
Now, if I select bank the other dropdown should populate items related to bank and if I select cash, cash items should be shown in 2nd dropdown.
How to do it in JSF? Where I have to make changes?
Any help will be highly appreciated.Thanks
Well you can use primefaces to improve jsf development
<p:selectOneMenu id="cboCountries" value="#{beanCountries.ValueSelected}" effect="fade">
<f:selectItem itemLabel="Select Country" itemValue="-1" /> //a default value
<f:selectItems value="#{beanCountries.listOfCountries}" var="countries" itemLabel="#{countries.name}" itemValue="#{countries.id}"/>
<p:ajax event="change" update="cboCities" listener="beanCountries.listCities()"/> //notice "update" refresh an especific DOM Element of an especific ID
</p:selectOneMenu>
<p:selectOneMenu id="cboCities" value="#{beanCities.ValueSelected}" effect="fade">
<f:selectItem itemLabel="Select City" itemValue="-1" /> //a default value
<f:selectItems value="#{beanCities.listOfCities}" var="cities" itemLabel="#{cities.name}" itemValue="#{cities.id}"/> //this is when you want to put elements from DB or from an Array
</p:selectOneMenu>
" is a special mark from primefaces, the event = "change" will invoke the you select an element from the first "ComboBox" then the property update will refresh the second comboBox, and the property "listener" will do the logical action of what you want to do, in this case the method "listCities()" will fill "listOfCities" with values from DB o Arrays.
So in jsf without primefaces will be like this:
<h:form>
<h:selectOneMenu value="#{beanCountries.ValueSelected}">
<f:selectItem itemLabel="Select Country" itemValue="-1" /> //a default value
<f:selectItems value="#{beanCountries.listOfCountries}" var="countries" itemLabel="#{countries.name}" itemValue="#{countries.id}"/>
</h:selectOneMenu>
<h:selectOneMenu id="cboCities" value="#{beanCities.ValueSelected}">
<f:selectItem itemLabel="Select City" itemValue="-1" /> //a default value
<f:selectItems value="#{beanCities.listOfCities}" var="cities" itemLabel="#{cities.name}" itemValue="#{cities.id}"/> //this is when you want to put elements from DB or from an Array
</h:selectOneMenu>
<h:commandButton value="Submit" action="beanCountries.listCities()"/>
</h:form>
this way is with a button (in jsf) and the action property will execute a java Method and then refresh the page. also this could be made by "ValueChangeListener" property of the selectOneMenu component

Resources