not getting expected parameter when selectOneMenu item is selected - jsf-2

Hi I have the following in my HTML:
<p:selectOneMenu value="#{lottoCheckerBean.selectedPowerBallDrawingDate}" >
<p:ajax update="powerBallDrawings" listener="#{lottoCheckerBean.handleDrawDateSelection}" />
<f:selectItems value="#{lottoCheckerBean.powerBallDrawingDates}" />
</p:selectOneMenu>
The relavent section of my bean code follows:
public void setSelectedPowerBallDrawingDate(String newSelectedPowerBallDate) {
this.selectedPowerBallDrawingDate = newSelectedPowerBallDate;
}
The ajax tag causes a call to the setSelectedPowerBallDrawingDate(String newSelectedPowerBallDate) as expected. But the value of newSelectedPowerBallDate is the empty string. Not the expected selected value of the dropdown.
The source for the selectOneMenu is a List<String>
List. Does this require a converter?

Thanks to Laabidi Raissi for his suggestion. The answer, however was that my selectOneMenu fell outside of the form. Moving the form tags so they enclosed the dropdown solved the problem.

Related

PrimeFaces selectOneMenu have null new value in both valueChangeListener and ajax listener

I've got a problem with PrimeFaces. Here is my XHTML code :
<p:selectOneMenu value="#{gCnewObjectBean.grille.branche}"
valueChangeListener="#{gCnewObjectBean.brancheChangedListener}"
update=":contentform :popup">
<f:selectItems value="#{gCnewObjectBean.branche}" />
<p:ajax update=":contentform :popup"
listener="#{gCnewObjectBean.brancheChangedListener}" />
</p:selectOneMenu>
And here is my bean code :
public void brancheChangedListener(ValueChangeEvent event) {
Integer branche = (Integer) event.getNewValue();
// do populate the second select menu based on this value
}
public void brancheChangedListener(AjaxBehaviorEvent event) {
SelectOneMenu selectMenu = (SelectOneMenu) event.getSource();
Integer branche = (Integer) selectMenu.getSubmittedValue();
// do populate the second select menu based on this value
}
The HTML code generated is a bit strange with primefaces, the HTML select component is hidden. But if I see it with firebug, it looks like this :
<select id="branche_input" name="branche_input">
<option value="0"></option>
<option value="1">Prestations prévoyance professionnelle</option>
<option value="2">Prestations prévoyance privée</option>
<option value="3">Prestations PRIRENT</option>
<option value="4">Gestion prévoyance professionnelle</option>
</select>
This code is a way to test the two solutions (valueChangeListener and the f:ajax) described by BalusC on this post.
When I do change the value in the select menu, both listener are triggered, first the valueChangeEvent, then the AjaxBehaviorEvent, which is perfectly what I suspected based on BalusC post. But both bean method do get a "null" for the new value (with debugger, I can see that the old value in the ValueChangeEvent is "0", which is correct according to my selectItems).
Any clue to solve this ? I really dont get it, I saw the code I'm using several time on this site...
Technical info : JBoss EAP 6.0, Mojarra 2.1.7-jboss, PrimeFaces 4.0
Edit : I was wondering if it was a bug in the jsf-impl library embedded in JBoss. I updated to Mojarra 2.1.18-jboss, but the problem is strictly the same : a null value is placed into my grille object by the ajax listener and a null value is also returned by the getNewValue() object of the valueChangeEvent.
First of all, i think there is a problem with your f:selectItems tag. Try to set the itemValue and itemLabel attributes for it: (I think the gCnewObjectBean.branche is not a integerList,is it?)
<p:selectOneMenu value="#{gCnewObjectBean.grille.branche}"
valueChangeListener="#{gCnewObjectBean.brancheChangedListener}"
update=":contentform :popup">
<f:selectItems value="#{gCnewObjectBean.brancheList}" var="item"
itemValue="#{item.branche}" itemLabel="#{item.label}"/>
<p:ajax update=":contentform :popup"
listener="#{gCnewObjectBean.brancheChangedListener}" />
Your attempt to get the selected value with the getSubmittedValue() method can't work. getSubmittedValue is not returning the value you are expecting:
See Why can I not get the submitted value from component binding?
If this is not the solution to your problem, you can try to put a p:messages tag in your jsf page. Probably there are errors which does not show up in the console.

can't refresh a selectOneMenu

first excuse my english if it's not correct ...
I've a probleme with a primeface's component, I'm trying to refresh a p:selectOneMenu from a p:commandButton, but it's doesn't work (it 's work on another xhtml page, but not here and I can't understand why ...)
First I select an item from an p:autocomplete which update a backingbean's attribute ( for example : userChoose ).
then the p:commandButton is able to call his listener and add userChoose to a list, but i can't refresh the selectOneMenu that display the list. I have to use another p:commandButton to refresh the list.
My form is included into a p:tabMenu in another xhtml page.
<p:autoComplete id="acPojo" value="#{forumBean.user}"
completeMethod="#{autoCompleteBean.completeUser}"
converter="#{userConverter}" forceSelection="true"
var="usr" itemLabel="#{usr.loginUtilisateur}" itemValue="#{usr}">
<p:column>
<h:outputText value="#{usr.loginUtilisateur}"/>
</p:column>
</p:autoComplete>
<p:commandButton value="ajouter" process="acPojo #this "
udpate=":tabView:formSujet:listeUser" actionListener="#{forumBean.addUser}"/>
<p:selectOneMenu value="#{forumBean.user}" converter="#{userConverter}" var="us" id="listeUser"
itemValue="#{us}" itemLabel="#{us.loginUtilisateur}">
<f:selectItems value="#{forumBean.newSujet.listeUserAllowed}" var="User"
itemValue="#{User}" itemLabel="#{User.loginUtilisateur}" />
<p:column>
<h:outputText value="#{us.loginUtilisateur}"/>
</p:column>
<p:ajax process="#this" />
</p:selectOneMenu>
<p:commandButton id="refreshAdmin" icon="ui-icon-arrowrefresh-1-w"
update=":tabView:formSujet:listeUser" />
Thanks for help.
From your code:
udpate=":tabView:formSujet:listeUser"
This has at least 2 (potential) mistakes. The right attribute name is update, not udpate. The one on your other button has however the right attribute name.
It that still doesn't work, then the other potential mistake is that the client ID tabView:formSujet:listeUser does not exist in HTML DOM tree (and thus JavaScript/jQuery is unable to find and replace it). That can happen if the <p:tabView> is dynamic (i.e. you're using <p:tabView value="#{bean.tabs}" var="tab">, because it prepents the tab index number in the final client ID like so tabView:0:formSujet:listeUser if it's the 1st tab.
But, after all, as both the dropdownlist and the commandbutton are in the same NamingContainer parent, you don't need an absolute client ID at all. Just the relative client ID should suffice:
update="listeUser"
Fix that on your both buttons.
See also:
How to find out client ID of component for ajax update/render? Cannot find component with expression "foo" referenced from "bar"

JSF / Primefaces 3.4.1 / SelectOneMenu / Pojo / Pre select pojo value not work

This preselected autoCompleteBean.selectedPlayer1 does not work !!
This is the example extracted from the showcase of primefaces site: https://www.primefaces.org/showcase/ui/input/oneMenu.xhtml
<p:selectOneMenu value="#{autoCompleteBean.selectedPlayer1}" converter="player">
<f:selectItems value="#{autoCompleteBean.players}" var="player"
itemLabel="#{player.name}" itemValue="#{player}"/>
</p:selectOneMenu>
In the bean, I put this lines :
private Player selectedPlayer1;
private List players;
/* AutoCompleteBean - constructor */
public AutoCompleteBean() {
players = new ArrayList<Player>();
players.add(new Player("Messi", 10, "messi.jpg", "CF"));
players.add(new Player("Bojan", 9, "bojan.jpg", "CF"));
selectedPlayer1 = players.get(1);
}
Variable autoCompleteBean.selectedPlayer1,
contain the value that you specify before opening the jsp.
But, this is not preselected. Only appear always selected the first element
of the arraylist. Why ??
Only I need that p:selectOneMenu, preselect de value in the list.
Thanks in advance !!
Well, in fact you can pre-select a value, or in other words, select a value by default by doing this:
<p:selectOneMenu value="#{autoCompleteBean.selectedPlayer1}" effect="fade" converter="player">
<f:selectItem itemLabel="autoCompleteBean.selectedPlayer1.name" itemValue="autoCompleteBean.selectedPlayer1" />
<f:selectItems value="#{autoCompleteBean.players}" var="player" itemLabel="#{player.name}" itemValue="#{player}"/>
</p:selectOneMenu>
The only problem with this is that it will repeat the value selected-by-default like this:
And you I guess you would like something like this:
So for this matter, i will recommend you to use the array player:
itemValue="#{player}"
but removing from this array the pre-selected option
Does your player class have hashCode() and equals() functions?
If they are missing, primefaces cannot make differences between them.
Similar problem:
primefaces selectOneMenu doesn't working when it should

Select Item from many menu

Ok I have a simple many menu in wich I call a listener
<p:selectManyMenu style="width: 100%;" id="cmbsectores" valueChangeListener="#{mbcompletado.removeItem}">
<f:selectItems value="#{mbcompletado.sectores}"/>
<f:ajax update="#this"/>
</p:selectManyMenu>
I am looking the way I can use the ValueChangeEvent pass as parameter to detect which item was selected??
So I can use my business logic!
Do i need to use ajax tag? I found an itemSelect event in primeface, framework which I am using, but it only works on charts components!
Thanks in advance
Since you are already using PrimeFaces use p:ajax instead of f:ajax. The event is already set to the appropriate event (valueChanged).
To detect the selected values of the selectManyMenu the value attribute is necessary:
<p:selectManyMenu style="width: 100%;" id="cmbsectores"
value="#{mbcompletado.selectedSectores}">
<f:selectItems value="#{mbcompletado.sectores}"/>
<p:ajax/>
</p:selectManyMenu>
You can remove the valueChangeListener listener altogether.
For a more complete example see SelectManyMenu.
EDIT:
In your backing bean mbcompletado.selectedSectores should point to a collection of the same type like your mbcompletado.sectores. For example, if your sectores is a List of TypeA, selectedSectores should be also a List of the same type (TypeA).
Similar backing-bean structure can be found in the following example SelectManyCheckbox.
You need the <f:ajax listener> (or in this case better <p:ajax listener>) method instead. The ValueChangeListener serves an entirely different purpose and should only be used when you're really interested in both the old and new value, not when you're only interested in the new value.
E.g.
<p:selectManyMenu value="#{bean.selectedSectors>
<f:selectItems value="#{bean.availableSectors}"/>
<p:ajax listener="#{bean.selectedSectorsChanged}" />
</p:selectManyMenu>
with
private List<String> selectedSectors;
private List<String> availableSectors;
public void selectedSectorsChanged() {
System.out.println("Selected sectors are: " + selectedSectors); // Look, JSF has already set it.
// ...
}
See also:
When to use valueChangeListener or f:ajax listener?

set itemDisabled from backing bean method for each item in list

I have a radiobutton list and would like to disable some items according to the outcome of a backing bean method.
<h:selectOneRadio value="#{managedBean.selectedItem}">
<f:selectItems value="#{managedBean.selectItems}"
var="x"
itemDisabled="#{managedBean.checkIncompatible(x)}" />
</h:selectOneRadio>
Is this the right way to do it? Meaning, will this code call checkIncompatible(x) for each x from the selectItems list and set that item as enabled/disabled or just once and that's that?
I only managed to get all buttons to be either enabled or disabled and my suspicion is that the method only gets called once. Or that the rest of my code is not as perfect as I like to believe. And that would take a much longer question to fix.
I can't reproduce your problem on Mojarra 2.1.4 with the following view:
<h:selectOneRadio value="#{bean.item}">
<f:selectItems value="#{bean.items}" var="item"
itemDisabled="#{bean.isDisabled(item)}" />
</h:selectOneRadio>
and the following bean:
private String[] items = { "one", "two", "three" }; // +getter
private String item; // +getter+setter
public boolean isDisabled(String item) {
return "two".equals(item);
}
The above example correctly disables item two.
So, your problem is caused elsewhere, perhaps by a bug in checkUncompatible() method. A breakpoint on the method teaches me that it's definitely called for each item.

Resources