如何在 ajax 调用中维护参数? [英] How can I maintain param on ajax call?
问题描述
每当我进行 ajax 调用时,我的 URL 参数就会过期.我所做的解决方法是在每个按钮内传递一个参数请求,如:
Whenever I make an ajax call, my URL param expires. The workaround I have done is to pass a param request inside every button like in:
<p:commandLink value="Submit" actionListener="#{mybean.dosomething}">
<f:param name="foo" value="#{mybean.bar}"/>
</p:commandLink>
但是,在某些情况下,我无法执行上述解决方法.例如,当我使用 primefaces rowEditEvent 时:
However, in some scenarios I can't do the above workaround. For example when I'm using primefaces rowEditEvent:
<p:ajax event="rowEditCancel" listener="#{mybean.onCancel}"/>
当我调用 #{mybean.onCance}
之前,当我进行这个 ajax 调用并导致错误时,参数会过期,因为我正在从 URL 中的参数读取数据表的数据.
The param expires when I make this ajax call and results in error before invoking #{mybean.onCance}
as I'm reading datatable's data from the param in URL.
那么当我进行这样的ajax调用时如何保持param值?
So how can I maintain the param value when I make such an ajax call?
PS:mybean 是 ViewScoped
PS: mybean is ViewScoped
问题扩展:
<o:form>
已经解决了部分问题,但是现在我无法在表格内发送参数以在表格内进行动态图像流传输.请参阅以下内容:
The <o:form>
has solved part of the problem, but now I can't send params inside the form for dynamic image streaming inside the table. See the following:
<p:dataTable value="#{mybean.data}" var="var">
<p:column headerText="Thumbnail">
<p:graphicImage value="#{streamer.thumbnail}">
<f:param name="id" value="#{var.id}"/>
</p:graphicImage>
</p:column>
</p:dataTable>
Streamer Bean (RequestScoped):
Streamer Bean (RequestScoped):
public StreamedContent getThumbnail() throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
if (context.getRenderResponse()) {
return new DefaultStreamedContent();
}
else {
String string = context.getExternalContext().getRequestParameterMap().get("id");
Long id = Long.parseLong(idString);
MyImage img = (MyImage) service.find(MyImage.class, id);//get resource
StreamedContent sc = new DefaultStreamedContent(img.getInputStream(), "image/jpg", img.getName());
return sc;
}
}
这里 string
总是为 null 并且没有传递参数导致错误
Here string
is always null and parameter is not passed resulting in error
推荐答案
将其注册为
<f:viewParam name="foo" value="#{mybean.bar}" />
而不是
,使用 OmniFaces
和 includeViewParams="true"
(在幕后使用自定义 ViewHandler
实现,该实现自动收集所有视图参数并将它们附加到用作表单操作 URL 的 getActionURL()
方法的结果):
and instead of <h:form>
, use OmniFaces <o:form>
with includeViewParams="true"
(which uses under the covers a custom ViewHandler
implementation which automatically collects all view parameters and appends them to the outcome of the getActionURL()
method which is used as form action URL):
<o:form includeViewParams="true">
...
</o:form>
通过这种方式,所有视图参数都包含在表单的操作 URL 中,因此将以通常的方式作为请求参数结束,而无需摆弄 <f:param>
并且在 <f:param>
中毫无头绪代码>
).
This way all view params are included in the form's action URL and will therefore end up as request parameters the usual way without the need to fiddle with <f:param>
and being clueless inside <p:ajax>
(and <f:ajax>
).
这篇关于如何在 ajax 调用中维护参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!