如何在ajax调用中保持参数?

编程入门 行业动态 更新时间:2024-10-24 18:25:38
本文介绍了如何在ajax调用中保持参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

每当我进行ajax调用时,我的URL参数就会过期.我所做的解决方法是在每个按钮内传递一个param请求,例如:

<p:commandLink value="Submit" actionListener="#{mybean.dosomething}"> <f:param name="foo" value="#{mybean.bar}"/> </p:commandLink>

但是,在某些情况下,我无法执行上述解决方法.例如,当我使用primefaces rowEditEvent时:

<p:ajax event="rowEditCancel" listener="#{mybean.onCancel}"/>

当我进行此Ajax调用时,该参数将过期,并在调用#{mybean.onCance}之前导致错误,因为我正在从URL的参数中读取数据表的数据.

那么当我进行这样的ajax调用时,如何保持param值呢?

PS:mybean是ViewScoped

问题扩展:

<o:form>已经解决了部分问题,但是现在我无法在表单内发送参数以在表内进行动态图像流传输.请参阅以下内容:

<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):

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,并且未传递参数导致错误

解决方案

将其注册为<f:viewParam>

<f:viewParam name="foo" value="#{mybean.bar}" />

,而不使用<h:form>,请使用 OmniFaces <o:form> 和includeViewParams="true"(在幕后使用了自定义ViewHandler实现,该实现会自动收集所有视图参数并将它们附加到getActionURL()的结果中用作表单操作网址的方法):

<o:form includeViewParams="true"> ... </o:form>

这样,所有视图参数都包含在表单的操作URL中,因此将以通常的方式作为请求参数结束,而无需摆弄<f:param>并且在<p:ajax>(和<f:ajax>)中毫无头绪. /p>

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>

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}"/>

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.

So how can I maintain the param value when I make such an ajax call?

PS: mybean is ViewScoped

Problem Extension:

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):

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; } }

Here string is always null and parameter is not passed resulting in error

解决方案

Register it as a <f:viewParam>

<f:viewParam name="foo" value="#{mybean.bar}" />

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>

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调用中保持参数?

本文发布于:2023-07-15 10:28:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110662.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   如何在   ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!