When i tried to send some values with h:commandButton… i recieved null value in the bean…
my xhtml code is:
<p:commandButton action="#{favouriteAction.setFavourite}" value="Add as Favorite" rendered="#{favouriteBean.favouriteButton}">
<f:setPropertyActionListener target="#{favouriteAction.ngoID}" value="#{InsertDataDaoService.ngo_id}"></f:setPropertyActionListener>
</p:commandButton>
In the backing bean i just tried to print the value which i passed with my command button,but it becomes null…
In favouriteAction.java(My backing Bean)
public Integer ngoID;
public Integer getNgoID() {
return ngoID;
}
public void setNgoID(Integer ngoID) {
this.ngoID = ngoID;
}
public String setFavourite(){
System.out.println("Ngo id: "+ngoID);
System.out.println("Ngo id: "+getNgoID);
return "";
}
In console i dint get any exceptions, my o/p is
Ngo id: 0
Ngo id: 0
that is null, and it doesnt get passed..
The
<f:setPropertyActionListener>is evaluated during the request of the form submit, not during the request of displaying the form. So if its value#{InsertDataDaoService.ngo_id}is not preserved for that request, then it will fail.You have basically 2 options:
Ensure that
#{InsertDataDaoService.ngo_id}is preserved for the request of the form submit. How exactly do to that depends on the functional requirements which are not clear from the question. But generally, putting the#{InsertDataDaoService}bean in the view scope by@ViewScopedand making sure that you aren’t doing any business job in the getter method should be sufficient.Replace
<f:setPropertyActionListener>by<f:param>with@ManagedProperty.with
This way the value will be retrieved (and inlined as part of a JavaScript helper function of the
commandButton) during the request of displaying the form, not evaluated during the request of submitting the form.See also: