I am trying to set the value of <h:selectOneMenu> using the bean setter, but it is not working. Here is my .xml code:
<h:selectOneMenu value="#{adminActionController.tempBean.selectType}">
<f:selectItem itemLabel="Check" itemValue="Check" />
<f:selectItem itemLabel="Cash" itemValue="Cash"/>
<f:ajax event="change" listener="#{adminActionController.tempBean.changeType}"/>
</h:selectOneMenu>
And here is my bean code:
protected String selectType;
public String getSelectType() {
return selectType;
}
public void setSelectType(String selectType) {
this.selectType = selectType;
}
I tried a lot of ways, but something is still missing. I don't know what.
It sets h:inputText values to bean, but I have a problem with dropdown values.
Can anybody help me?
Try without immediate="true".And selectOneMenu must be inside h:form.When selectOneMenu is changed,your listener is working,right ?
Related
I'm using JSF 2.2.8 and primefaces 6.0, and i have a selectCheckBoxMenu i want to retrieve the selected values in my bean.
The selectCheckboxMenu is filled from the database but when i select the attributes and I save nothing happens it does not call the save function
Here is my selectCheckBoxMenu
<p:outputLabel for="ressource" value="Ressource"/>
<h:panelGroup >
<p:selectCheckboxMenu id="ressource" label="Ressource" value="#{affectationBean.selectedRessource}" multiple="true">
<f:selectItems value="#{affectationBean.ressources}" var="r" itemLabel="#{r.nom}" itemValue="r.idt_ressource" />
</p:selectCheckboxMenu>
</h:panelGroup>
<p:commandButton icon="ui-icon-save" actionListener="#{affectationBean.save}" value="Save" update="#affectation" ajax="false" style="display:inline-block;margin-top:5px"/>
Here is the the declaration of the selectedRessource and the actionListener save
private Long [] selectedRessource;
// Getters setters and Construct
public void save(){
for(int i=0 ;i<selectedRessource.length;i++){
system.out.println("id ===> " + selectedRessource[i]);
}
My suggestion would be:
First make sure everything is inside the h:form tag.
don't need to multiple = true as this tag does not take this attribute
i tested with below modification and got the selected multiple value in my bean. The only difference is i am using same value for itemLabel and itemValue but in your case it is object. i am using primefaces 6 also and dont even need to change actionListner to action. It is working as it is.sample xhtml
sample ResourceBean.java
<p:outputLabel for="ressource" value="Ressource"/>
<h:panelGroup >
<p:selectCheckboxMenu id="ressource" label="Ressource" value="#{resourceBean.selectedRessource}">
<f:selectItems value="#{resourceBean.ressources}" var="r" itemLabel="#{r}" itemValue="#{r}" />
</p:selectCheckboxMenu>
</h:panelGroup>
<p:commandButton icon="ui-icon-save" actionListener="#{resourceBean.save}" value="Save" ajax="false" style="display:inline-block;margin-top:5px"/>
The problem is in your p:commandButton, you have 3 options
change your method:
public void save(ActionEvent actionEvent){...}
change your action listener value:
actionListener="#{affectationBean.save()}"
or change your button to use action
action="#{affectationBean.save}"
DISCLAIMER: This is a workaround. It is not intended to be a permanent solution but will allow you to use selectCheckboxMenu and keep working.
There is an issue with this component that prevents it from passing values to the backing bean upon submit.
For some reason the array that should contain the selected values gets cleared out upon submit. Therefore I made an extra array that I did not declare in the tag, and updated in on every change event. Then on submit the values were still there. See below:
BackingBean.java
private String[] sCodes;
private String[] sCodes2; //extra array, not in form.xhtml
public void updateCodes()
{
sCodes2 = sCodes; //keeps the values in the other array
}
public void doSend() throws IOException
{
log.trace("selected codes: {} selected codes2 length: {}", sCodes.length, sCodes2.length);
}
form.xhtml
<p:selectCheckboxMenu id="codeCtl" value="#{bean.SCodes}" label="Codes" filter="true" filterMatchMode="startsWith" panelStyle="width:250px">
<f:selectItems value="#{bean.menuCodes}" />
<p:ajax event="change" listener="#{bean.updateCodes()}" />
</p:selectCheckboxMenu>
<p:commandButton value="submit" actionListener="#{bean.doSend}" id="ctlSubmit" update="appform"/>
I populated my selectOneMenu with objects and trying to send the id (in itemValue) back to my bean from the selected item, I tried it using the below functionality but I keep getting error about a null converter (Which I'm trying to avoid by sending the id to my bean).
xhtml:
<h:form>
<h:selectOneMenu value="#{bean.id}">
<f:selectItems value="#{bean.objectList}" var="f" itemValue="#{f.id}" itemLabel="#{f.name}" />
</h:selectOneMenu>
<h:commandButton action="#{bean.function}" value="OK"/>
</h:form>
bean:
private Collection<Object> objectList; //Object is an example, It is not the real class that is used
private int id;
public void function() {
// place where id is needed.
}
// id getters & setters
The collection you are using:
private Collection<Object> objectList;
Has a plain object as the class type for its elements.
You need to change it to:
private Collection<MyCustomClassElement> objectList;
Where MyCustomClassElement is the class for your collection elements used in the select.
I think the value is not updated, use ajax to update the value once you select it...
<h:form id="forms">
<h:selectOneMenu id="beanid" value="#{bean.id}">
<f:selectItems value="#{bean.objectList}" var="f" itemValue="#{f.id}" itemLabel="#{f.name}" />
<f:ajax event="change" render="forms" />
</h:selectOneMenu>
<h:commandButton action="#{bean.function}" value="OK"/>
</h:form>
This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
Closed 3 years ago.
I need some help. I'm developing for jsf and primefaces web application and I'm facing a problem when I'm selecting from a drop down list to get the selected value but I'm getting an empty string in the action.
This is my xhtml code for selectOneMenu tag
<p:selectOneMenu value="#{tanAllot.batchName}" id="batchName">
<f:selectItem itemLabel="Select Batch" itemValue="" />
<f:selectItems value="#{tanAllot.batchList}" />
<p:ajax event="change" listener="#{tanAllot.test}" />
</p:selectOneMenu>
this is the method I'm using in the action class
private String batchName;
public String getBatchName() {
return batchName;
}
public void setBatchName(String batchName) {
this.batchName = batchName;
}
public void test() {
System.out.println(batchName);
}
My problem is when I select a value from p:selectOneMenu tag the default method should invoke in the action and retrieve the value but I'm getting an empty string.
Can anyone help me to solve this problem?
Consider the nature of batchList. batchList must be List of Strings. Using itemValue attribute (in f:selectItem) can be helpful.
Check my example. It uses a list of provinces (instances of "Province" class). However I only need the "id" value which is a "Long"; if I wanted the whole picked province as a "Province object" I would need a "Converter". (Example works perfectly):
<p:selectOneMenu id="provinceField"
value="#{addAddressesMB.formAddress.provinceId}">
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItems value="#{addAddressesMB.provinceList}" var="i"
itemLabel="#{i.description}" itemValue="#{i.id}" />
<p:ajax update=":formId:cityField"
listener="#{addAddressesMB.provinceChangeHandler}" />
</p:selectOneMenu>
And here comes the listener method:
public void provinceChangeHandler() {
//do whatever you want with formAddress.provinceId
System.out.println(this.formAddress.provinceId);
//In my case I filter the cities according to the selected provinceId
// After that I update the cities dropdown(cityField) in the view.
}
Check your code and feel free to ask. Good luck.
I have several forms like this on the same page:
<h:form>
<h:selectOneMenu value="#{collectionBean.selectedCollection}">
<f:selectItems value="#{collectionBean.collectionItems}" />
<a4j:support event="onchange" />
</h:selectOneMenu>
<a4j:commandButton value="Add" action="#{collectionBean.addToCollection(_resource)}" >
</a4j:commandButton>
</h:form>
Here is my Bean:
#Name("collectionBean")
#Scope(ScopeType.SESSION)
public class CollectionBean {
private String selectedCollection;
public String getSelectedCollection() {
return selectedCollection;
}
public void setSelectedCollection(String collectionName) {
selectedCollection = collectionName;
}
public List<SelectItem> getCollectionItems() {
...
}
public void addToCollection(Resource res) {
...
}
}
A form is associated to a resource _resource, its goal is to let the user add the resource to a collection he choses.
The problem is that only the last form on the page works: when changing the selection in the other forms, the setSelectedCollection method is never called.
Do you have an idea of what could be wrong?
As said here and in the comments, it does not make sense to bind several components to the same bean property. So I used a Map in the backing bean, with the resource id as a key.
<h:selectOneMenu value="#{collectionBean.selections[_resource.id]}">
<f:selectItems value="#{collectionBean.collectionItems}" />
<a4j:support event="onchange" />
</h:selectOneMenu>
Still, it did not fix the main problem: only the last form on the page worked. For all the other forms, the method getSelections was never called.
Then, instead of using several forms (one form for each select menu), I used a single englobing form. I don't know why, but it worked...
I have a simple OneMenu in JSF:
#ManagedBean
#ViewScoped
public class ProductBean {
...
protected static Map<String, String> priceTypes;
...
getter & setter
}
<p:selectOneMenu id="sizeType" >
<f:selectItems value="#{productBean.priceTypes}" />
</p:selectOneMenu>
In my usecase, i want to preselect[1] an option out of "priceTypes" - how can i do that?
I am using Glassfish 3.1.2.2 with Primefaces 3.4.1
[1] See "selected" at http://www.w3schools.com/tags/tag_option.asp
I am not sure about pfaces, but in plain JSF you just have to set the value attribute in the selectOneMenu tag, and make sure that the value returned by the bean is in the select list.
<p:selectOneMenu id="sizeType" value="#{myBean.sizeType}>
<f:selectItems value="#{productBean.priceTypes}" />
</p:selectOneMenu>
Of course, sizeType must mutch the key of your map.