如何将参数传递给 h:inputText 中的 f:ajax?f:param 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10396244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass parameter to f:ajax in h:inputText? f:param does not work
提问by Shikha Dhawan
I need to pass a parameter to the server in my ajax request. Please see the code below. Scope: View Scope
我需要在我的 ajax 请求中向服务器传递一个参数。请看下面的代码。范围:查看范围
Without 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
托管 Bean
public final void onCostoBrutoChange(final AjaxBehaviorEvent event) {
createCostoBrutoOptions(promoArticlesList);
}
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.
在这种情况下,方法 onCostoBrutoChange() 确实被调用。但是,当我包含 f:param 时,它不会被调用。请看下面的代码。
With 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
托管 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.
无法识别此代码中的错误。请指导。
Thanks, Shikha
谢谢,希哈
回答by BalusC
The <f:param>works in links and buttons only, not in inputs.
该<f:param>只,而不是在输入链接和按钮的工作原理。
If your environment supports EL 2.2, just pass it as method argument instead:
如果您的环境支持 EL 2.2,只需将其作为方法参数传递即可:
<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) {
// ...
}
If your environment doesn't or can't support EL 2.2, then evaluate EL programmatically instead.
如果您的环境不支持或不能支持 EL 2.2,则改为以编程方式评估 EL。
public void listener() {
FacesContext context = FacesContext.getCurrentInstance();
Long id = context.getApplication().evaluateExpressionGet(context, "#{item.id}", Long.class);
// ...
}

