Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

jsf

jsf-2

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);
    }
}
like image 327
marioosh Avatar asked Jan 24 '11 13:01

marioosh


2 Answers

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.

like image 50
BalusC Avatar answered Oct 22 '22 07:10

BalusC


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>
like image 43
Benchik Avatar answered Oct 22 '22 06:10

Benchik