I have a problem with getting the selected value.
It is always 0.
XHTML file:
<p:selectOneMenu id="SelectDicimalsInput5" value="#{auction.money}">
<f:selectItem itemLabel="1" itemValue="1"/>
<f:selectItem itemLabel="5" itemValue="5"/>
<f:selectItem itemLabel="10" itemValue="10"/>
<f:selectItem itemLabel="100" itemValue="100"/>
<f:selectItem itemLabel="500" itemValue="500"/>
<f:selectItem itemLabel="1000" itemValue="1000"/>
</p:selectOneMenu>
Backing bean :
#ManagedBean (name="auction")
#RequestScoped
public class AuctionBean implements Serializable {
private int money;
//getters & setters ...
}
The problem here is that your selectonemenu selected value is a String and your variable in the backed bean is an int.
So this selected value can't be converted to int and that's why you always get 0 as the defailt value of the primitive type int here.
You have to convert this String into an int to match the variable type, you can use an IntegerConverter:
<f:convertNumber integerOnly="true" />
and your code will be:
<p:selectOneMenu id="SelectDicimalsInput5" value="#{auction.money}">
<f:selectItem itemLabel="1" itemValue="1"/>
<f:selectItem itemLabel="5" itemValue="5"/>
<f:selectItem itemLabel="10" itemValue="10"/>
<f:selectItem itemLabel="100" itemValue="100"/>
<f:selectItem itemLabel="500" itemValue="500"/>
<f:selectItem itemLabel="1000" itemValue="1000"/>
<f:convertNumber integerOnly="true" /> //This converter should be added
</p:selectOneMenu>
Also note that there's the converter="javax.faces.Integer" attribute that can be used with <h:selectOneMenu> elements but I think it's not supported by primefaces, but may be I am wrong, so you can try :
<p:selectOneMenu id="SelectDicimalsInput5" converter="javax.faces.Integer" value="#{auction.money}">
Related
I have a h:selectOneMenu which served me with no issues in the past but for some reason the value in searchResults.selectedCategories is always null when submitting the form.
The widget is inside a form. The backing bean has selectedCategories as a private String with accessor methods. I tried cleaning the project, closing down Eclipse, and republishing it to Tomcat. Nothing works. Any idea why?
This is the widget:
<h:selectOneMenu id="categoriesBoxSimple" value="#{searchResults.selectedCategories}" >
<f:selectItem itemLabel="Category 1" itemValue="283331" />
<f:selectItem itemLabel="Category 2" itemValue="281" />
<f:selectItem itemLabel="Category 3" itemValue="1115"/>
</h:selectOneMenu>
`Add`
<f:ajax listener="#{yourBean.ajaxChangeValue}" />
to h:selectOneMenu
<h:selectOneMenu value = "#{yourBean.numberValue}">
<f:selectItem itemValue="One" />
<f:selectItem itemValue="Two" />
<f:selectItem itemValue="Three" />
<f:ajax listener="#{yourBean.ajaxChangeValue}" />
</h:selectOneMenu>
YourBean.java
public void ajaxChangeValue(final AjaxBehaviorEvent event) {
// do something
}
//getter and setter of numberValue
I have a selectOneMenu, in my xhtml page, when i want to click in a selectItem with the itemValue is null, there is no effect, it shows the default selection
<p:selectOneMenu id="cout" style="width: 120px;" value="#{serviceManagedBean.selectedService.coutSmsCalc}">
<f:selectItem itemLabel="Sélectionnez une" itemValue="" />
<f:selectItem itemLabel="oui" itemValue="oui" />
<f:selectItem itemLabel="non" itemValue="" />
</p:selectOneMenu>
So, when i click in the itemLabel "non", it remains on "Sélectionnez une"
The selectOneMenu use itemValue to change the displayed value. So if your value is null like the default one, the action changeListener is not called. Try to change the itemValue by empty or other key.
Try this if the item value is a String
<f:selectItem itemLabel="Sélectionnez une" itemValue="{null}" />
<f:selectItem itemLabel="oui" itemValue="oui" />
<f:selectItem itemLabel="non" itemValue="non" />
Or try this if the item value is boolean
<f:selectItem itemLabel="Sélectionnez une" itemValue="{null}" />
<f:selectItem itemLabel="oui" itemValue="true" />
<f:selectItem itemLabel="non" itemValue="false" />
When I run my application, "Single User" should be selected automatically.
<h:selectOneRadio onchange="showEmailDiv(this.id);" value="" id="emailValue" required="true">
<f:selectItem itemValue="Single" itemLabel="Single User" />
<f:selectItem itemValue="Multiple" itemLabel="Multiple User"/>
</h:selectOneRadio>
How can I preselect one radio button defaultly?
Set default value like this
<h:selectOneRadio onchange="showEmailDiv(this.id);" value="Single" id="emailValue" required="true">
<f:selectItem itemValue="Single" itemLabel="Single User" />
<f:selectItem itemValue="Multiple" itemLabel="Multiple User"/>
</h:selectOneRadio>
Note: you aren't binding this component with bean, not sure how would you use it
#ManagedBean
public class YourBean {
private String value = "Single User";
// getter & setter
}
And in your page
<h:selectOneRadio onchange="showEmailDiv(this.id);" value="#{yourBean.value}" id="emailValue" required="true">
<f:selectItem itemValue="Single" itemLabel="Single User" />
<f:selectItem itemValue="Multiple" itemLabel="Multiple User"/>
</h:selectOneRadio>
I have 2 components(select and inputText), in which values are dependent to each other. For example if "option 1" is selected then inputText must be numbers.
In my bean I have added attributes for 2 components for binding and a validation method, while in jsp i have added "validator" and "binding" attribute to select and "binding" to inputText.
I used binding to get the submitted value of both components for validation.
Is this the correct way? Is there an alternative to get the submitted value?
The result of doing this is duplicate message shown. If I remove binding attribute from select then it works as expected but I cannot fetch the selected value, rather is uses the cache value (bean value in session).
Thanks in advance.
aalmero
code:
<p:selectOneMenu
value="# {deploymentRequestViewBean.deploymentRequestDTO.deploymentRequest.requestLevel}"
id="requestLevel" required="true" label="requestLevel"
validator="#{deploymentRequestViewBean.validateRequestDate}">
<p:ajax listener="#{deploymentRequestViewBean.processRequestLevelValueChanged}"
binding="#{deploymentRequestViewBean.requestLevelSelectOne}"/>
<f:selectItem itemValue="" itemLabel="Select One" />
<f:selectItem itemValue="DEV" itemLabel="DEV" />
<f:selectItem itemValue="QUA" itemLabel="QUA" />
<f:selectItem itemValue="PRD" itemLabel="PRD" />
</p:selectOneMenu>
<p:calendar
value="#{deploymentRequestViewBean.deploymentRequestDTO.deploymentRequest.deployDate}"
id="deployDate" required="true" label="deployDate" showOn="button" pattern="yyyy- MM-dd" binding="#{deploymentRequestViewBean.requestDateInput}"/>
<p:spacer width="10" height="10" />
//for component-binding
private UISelectOne requestLevelSelectOne;
private UIInput requestDateInput;
//validation method
public void validateRequestDate(FacesContext facesContext,
UIComponent component, Object newValue){
//get the current value of select;
requestLevelSelectOne.getSubmittedValue();
//get the current vallue of input;
requestDateInput.getSubmittedValue()
if(not valid combination){
facesContext.addMessage(requestDateInput.getClientId(facesContext), new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", selectedLevel + " deployment request requires at least 2 days."));
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Deployment date must be at least 2 days."));
}
}
You can use a hack bypass by binding a hidden component value with the select component. In the "onchange" method of your <h:selectOneMenu> you can set the value of this hidden component and get the values in the server:
<h:form id="myForm">
<h:selectOneMenu id="cmbOptions"
onchange="document.getElementById('myForm:hidSelectOption').value=this.value">
<f:selectItem itemLabel="Option 1" itemValue="1" />
<f:selectItem itemLabel="Option 2" itemValue="2" />
<f:selectItem itemLabel="Option 3" itemValue="3" />
</h:selectOneMenu>
<h:inputHidden id="hidSelectOption" value="#{bean.selectedOption}" />
<h:commandButton value="Click me" action="#{bean.someAction}" />
</h:form>
The managed Bean
#RequestScope
#ManagedBean
public class Bean {
private String selectedOption;
//getters and setters...
public Bean() {
}
public void someAction() {
//showing the actual value of the hidden component...
//remember that you should use a logger, this is a basic example
System.out.println(selectedOption);
}
}
Is there a way to replace this in backing bean
private int room1ad
private int room1ch
private int room1ch1
private int room1ch2
private int room1ch3
private int room1ch4
// getters & setters
and this in the view
<h:form>
<h:selectOneMenu value="#{hotelsController.room1ad}">
<f:selectItem itemLabel="1" itemValue="1"/>
<f:selectItem itemLabel="2" itemValue="2"/>
<f:selectItem itemLabel="3" itemValue="3"/>
</h:selectOneMenu>
<h:selectOneMenu value="#{hotelsController.room1ch}">
<f:selectItem itemLabel="1" itemValue="1"/>
<f:selectItem itemLabel="2" itemValue="2"/>
<f:selectItem itemLabel="3" itemValue="3"/>
</h:selectOneMenu>
<h:selectOneMenu value="#{hotelsController.room1ch1}">
<f:selectItem itemLabel="1" itemValue="1"/>
<f:selectItem itemLabel="2" itemValue="2"/>
<f:selectItem itemLabel="3" itemValue="3"/>
</h:selectOneMenu>
......
</h:form>
This doesn't look so bad, but I have 10 rooms in one backing bean.
I thought about something like this in backing bean
//BB
private Room room1
And the view basically the same, but it would create object after submition
so the way it works instead of having 6 ints for each room in BB I would only have x Room classes inside and XHTML form would make directly POJO instead of accessing individually each int.
EL supports lists and properties on POJOs, so you could easily use it:
public List<Room> getRooms();
and xhtml:
<ui:repeat value="#{hotelsController.rooms}" var="room">
<h:selectOneMenu value="#{room.ad}">
<f:selectItem itemLabel="1" itemValue="1"/>
<f:selectItem itemLabel="2" itemValue="2"/>
<f:selectItem itemLabel="3" itemValue="3"/>
</h:selectOneMenu>
.
.
</ui:repeat>