How to pass additional parameters in ajax request to the value of the change in h: selectOneMenu?

I need to pass some parameters (id in my example) to the f: ajax listenener method, but I do not know how to do this. Can 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); } } 
+3
source share
2 answers

Passing parameters 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> 
+5
source

It was sent as a request parameter named id . So to the point (and hacks):

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

If the bean is the scope of the request, you can also make this a managed property.

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

There may be better ways depending on where #{id} really occurs, which is unclear based on the information given so far in the question. There are situations when you do not need to pass it as a request parameter at all.

+5
source

Source: https://habr.com/ru/post/1400804/


All Articles