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

╄→尐↘猪︶ㄣ 提交于 2019-12-29 00:36:51

问题


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);
    }
}

回答1:


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>



回答2:


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.



来源:https://stackoverflow.com/questions/4782430/how-to-pass-additional-parameters-in-ajax-request-on-change-value-in-hselectone

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!