2012-05-01 16 views
9

我需要在我的ajax請求中將參數傳遞給服務器。請參閱下面的代碼。 範圍:查看範圍如何將參數傳遞給h:inputText中的f:ajax? f:param不起作用

沒有F:PARAM

<p:column width="40"> 
    <h:inputText id="originalCostInputTxt" value="#{articlePromo.costoBruto}" 
     <f:ajax event="change" 
      execute="@this" 
      listener="#{promotionDetailManagedBean.onCostoBrutoChange}"> 
     </f:ajax> 
    </h:inputText> 
</p:column> 

Managed Bean的

public final void onCostoBrutoChange(final AjaxBehaviorEvent event) { 
    createCostoBrutoOptions(promoArticlesList); 
} 

在這種情況下,方法onCostoBrutoChange()不會被調用。但是,當我包含f:param時,它不會被調用。請參閱下面的代碼。

使用f:PARAM

<p:column width="40"> 
    <h:inputText id="originalCostInputTxt" value="#{articlePromo.costoBruto}" 
     <f:ajax event="change" 
      execute="@this" 
      listener="#{promotionDetailManagedBean.onCostoBrutoChange}"> 
     <f:param value="#{articlePromo.promocionArticuloId}" name="myId"/> 
     </f:ajax> 
    </h:inputText> 
</p:column> 

Managed Bean的

public final void onCostoBrutoChange(final AjaxBehaviorEvent event) { 
    createCostoBrutoOptions(promoArticlesList); 
    String id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("myId"); 
} 

無法確定這個代碼什麼不正確。請指導。

感謝, Shikha

回答

27

<f:param>作品只,而不是在輸入鏈接和按鈕。

如果環境支持EL 2.2,只是把它作爲方法的參數改爲:

<h:inputText ...> 
    <f:ajax listener="#{bean.listener(item.id)}" /> 
</h:inputText> 

public void listener(Long id) { 
    // ... 
} 

你也可以通過全項:

<h:inputText ...> 
    <f:ajax listener="#{bean.listener(item)}" /> 
</h:inputText> 

public void listener(Item item) { 
    // ... 
} 

如果您的環境不支持或不支持EL 2.2,請改爲以編程方式評估EL。

public void listener() { 
    FacesContext context = FacesContext.getCurrentInstance(); 
    Long id = context.getApplication().evaluateExpressionGet(context, "#{item.id}", Long.class); 
    // ... 
} 
+0

不知道這種方式...不錯! – Daniel

+3

上帝自己的答案.. :)謝謝。有效。沒有意識到這一點。 –

+0

@BalusC我在'c:foreach'裏面用編程的方式評估EL,使用迭代變量'var',但它的計算結果爲null。 – Theo

相關問題