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>
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 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}">
I am trying to display a menu if a radiobutton is selected. I'm using the rendered attribute for this purpose.
Model:
private int type; // +getter+setter
View:
<h:selectOneRadio value="#{bean.type}">
<f:selectItem itemLabel="A" itemValue="1"/>
<f:selectItem itemLabel="B" itemValue="2"/>
</h:selectOneRadio>
<h:form id="formMention" rendered="#{bean.type == 1}">
<h:selectOneMenu ...>
<f:selectItems ... />
</h:selectOneMenu>
</h:form>
Nothing shows when I check A. How can I achieve this?
Normally, you'd grab ajax for this, but as you're apparently on dead JSF 1.x, which lacks ajax fanciness, you're resorted to "plain" HTML/JS as long as you don't want to introduce an ajax capable component library. One of the ways is just submitting the form outright on click of the radio button.
<h:form>
<h:selectOneRadio value="#{bean.type}" onclick="this.form.submit()">
<f:selectItem itemLabel="A" itemValue="1"/>
<f:selectItem itemLabel="B" itemValue="2"/>
</h:selectOneRadio>
</h:form>
<h:form id="formMention">
<h:selectOneMenu ... rendered="#{bean.type == 1}">
<f:selectItems ... />
</h:selectOneMenu>
</h:form>
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 following selectOneMenu
<p:selectOneMenu value="#{bean.value}">
<f:selectItem value="#{bean.item1}"/>
<f:selectItem value="#{bean.item2}"/>
<f:selectItem value="#{bean.item3}"/>
<p:ajax listener="#{bean.item3AjaxEvent}" update="fieldToUpdate"></p:ajax>
</p:selectOneMenu>
Now I want to do some AJAX action only when item3 is selected from selectOneMenu. Not for all the items. Is there any way of doing that?
Putting ajax tag will fire the event for all the select items. I don't want to generate unwanted ajax requests to server.
I would do it in this way.
xhtml
<p:selectOneMenu widgetVar="selectOneMenuWV"
onchange="checkItem()">
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItem itemLabel="Option 1" itemValue="1" />
<f:selectItem itemLabel="Option 2" itemValue="2" />
<f:selectItem itemLabel="Option 3" itemValue="3" />
</p:selectOneMenu>
<p:remoteCommand name="myRemoteCommand"
actionListener="#{bean.item3AjaxEvent()}"
update="fieldToUpdate"/>
<script>
function checkItem() {
if(selectOneMenuWV.getSelectedValue() == 3) {
myRemoteCommand();
}
}
</script>
Hope this helps.