Karthikeyan
Karthikeyan

Reputation: 767

Sending values with <h:commandbutton> not working working properly in jsf

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..

Upvotes: 1

Views: 2775

Answers (2)

BalusC
BalusC

Reputation: 1108537

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:

  1. 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 @ViewScoped and making sure that you aren't doing any business job in the getter method should be sufficient.

  2. Replace <f:setPropertyActionListener> by <f:param> with @ManagedProperty.

    <p:commandButton action="#{favouriteAction.setFavourite}" value="Add as Favorite" rendered="#{favouriteBean.favouriteButton}">
        <f:param name="ngoID" value="#{InsertDataDaoService.ngo_id}" />
    </p:commandButton>
    

    with

    @ManagedProperty("#{param.ngoID}")
    private Integer ngoID;
    

    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:

Upvotes: 1

Forty
Forty

Reputation: 430

Have you checked that the value of InsertDataDaoService.ngo_id is not NULL? Try to replace it with a constant value. Does it work?

Upvotes: 1

Related Questions