0

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

Karthikeyan
  • 757
  • 3
  • 11
  • 25
  • `favouriteBean` is request scoped? It should be at least view scoped to make this `rendered="#{favouriteBean.favouriteButton}"` work properly. There are werid problems with this. – Piotr Gwiazda Jan 19 '12 at 11:16
  • i have no problem in rendering the command button, its properly rendered even after that bean is request scoped. my problem is with passing an value with commandbutton.... – Karthikeyan Jan 19 '12 at 11:22
  • I had serveral problems with `rendered` and button commands. Button is rendered OK but problems exits on Restore View phase. On RestoreView JSF creates new request bean with `favouriteButton` set to false and it looks like button is not rendered. Werid things happens here. Probably `setPropertyActionListener` is not created at all, because it's inside not rendered component. On update model phase `favouriteButton` is true and the button is rendered properly. But it's too late: `setPropertyActionListener` was missed. – Piotr Gwiazda Jan 19 '12 at 12:54
  • even i tried removing render option but no use. – Karthikeyan Jan 19 '12 at 13:05
  • @Peter: the action is invoked. So the `rendered` attribute isn't the problem. – BalusC Jan 19 '12 at 13:19

2 Answers2

1

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

Forty
  • 420
  • 1
  • 5
  • 10
1

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555