5

I need to pass some parameters (id in my example) to f:ajax listener method, but i don't know how. Anybody help ?

<h:form>
    <!-- need to pass id value -->
    <input type="hidden" name="id" id="id" value="#{id}"/>

    <h:selectOneMenu value="#{visibility}">
      <f:selectItems value="#{visibilities}" var="e" itemValue="#{e}" itemLabel="#{e.name}" />
      <f:ajax event="valueChange" render="@form" execute="@form" listener="#{bean.updateVisibility}" />         
    </h:selectOneMenu>
</h:form>

Bean:

class Bean {
    Integer id;

    public void setId() {
       this.id = id;
    }

    public void updateVisibility(AjaxBehaviorEvent event) { 
       // passed id
       log.debug(id);
    }
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
marioosh
  • 27,328
  • 49
  • 143
  • 192

2 Answers2

8

It's been sent as request parameter with the name id. So, to the point (and hacky):

String id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id");

If the bean is request scoped, you can also make it a managed property.

@ManagedProperty(value="#{param.id}")
private Integer id; // +setter

There may be better ways depending on where the #{id} actually originate, which is yet unclear based on the as far given information in the question. There are namely situations where you don't need to pass it around as request parameter at all.

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

Passing params to f:ajax is done by:

<f:ajax event="valueChange" render="@form" execute="@form" listener="#{bean.updateVisibility}">
    <f:param value="#{id}" name="myId">
</f:ajax>
Benchik
  • 2,057
  • 5
  • 26
  • 44
  • 6
    In my experience the doesn't work within tag. It should be directly inside the UICommand component. Please see this http://stackoverflow.com/questions/11832607/fajax-doesnt-work-when-parameters-are-passed-using-fparam/11833612#11833612 – Paras Aug 06 '12 at 18:31
  • In addition to the above comment, the basic thing that the param tag should have an end tag. – Ganesa Vijayakumar Oct 05 '17 at 07:58