2011-01-24 77 views
3

我需要傳遞一些參數(在我的例子中的id)f:ajax監聽器方法,但我不知道如何。有人幫忙嗎?如何在h中更改值的ajax請求中傳遞附加參數:selectOneMenu?

<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> 

豆:

class Bean { 
    Integer id; 

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

    public void updateVisibility(AjaxBehaviorEvent event) { 
     // passed id 
     log.debug(id); 
    } 
} 

回答

5

傳遞PARAMS到f:AJAX是通過做:

<f:ajax event="valueChange" render="@form" execute="@form" listener="#{bean.updateVisibility}"> 
    <f:param value="#{id}" name="myId"> 
</f:ajax> 
+5

在我的經驗,標籤內不起作用。它應該直接在UICommand組件的內部。請看這個http://stackoverflow.com/questions/11832607/fajax-doesnt-work-when-parameters-are-passed-using-fparam/11833612#11833612 – Paras 2012-08-06 18:31:05

5

已被髮送的請求參數的名稱id。所以,給點(和哈克):

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

如果bean是請求範圍,你也可以讓它託管屬性。

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

可能有不同的地方的#{id}實際上起源,這是目前還不清楚基於問題儘量給出的信息更好的方法。有些情況下你根本不需要傳遞它作爲請求參數。