如何将参数传递给 h:inputText 中的 f:ajax?f:param 不起作用 [英] How to pass parameter to f:ajax in h:inputText? f:param does not work

查看:21
本文介绍了如何将参数传递给 h:inputText 中的 f:ajax?f:param 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的 ajax 请求中向服务器传递一个参数.请看下面的代码.范围:查看范围

I need to pass a parameter to the server in my ajax request. Please see the code below. Scope: View Scope

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

托管 Bean

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

在这种情况下,方法 onCostoBrutoChange() 确实被调用.但是,当我包含 f:param 时,它不会被调用.请看下面的代码.

In this case, the method onCostoBrutoChange() does gets invoked. But, it does not get invoked when I include f:param. Please see the code below.

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

托管 Bean

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

无法识别此代码中的错误.请指导.

Not able to identify whats incorrect in this code. Please guide.

谢谢,什哈

推荐答案

仅适用于链接和按钮,不适用于输入.

The <f:param> works in links and buttons only, not in inputs.

如果您的环境支持 EL 2.2,只需将其作为方法参数传递即可:

If your environment supports EL 2.2, just pass it as method argument instead:

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

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

您也可以只传递整个项目:

You can also just pass the whole item:

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

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

如果您的环境不支持或不能支持 EL 2.2,请改为以编程方式评估 EL.

If your environment doesn't or can't support EL 2.2, then evaluate EL programmatically instead.

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

这篇关于如何将参数传递给 h:inputText 中的 f:ajax?f:param 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆