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);
}
}
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With