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

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

问题描述

我需要在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.

谢谢, 什卡(Shikha)

Thanks, Shikha

推荐答案

<f:param>仅适用于链接和按钮,不适用于输入.

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:参数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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