2

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

fareed
  • 3,034
  • 6
  • 37
  • 65

1 Answers1

4

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

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Cool!! The has solved part of the problem, but this resulted in another error as I can't send params inside the form for dynamic image streaming inside the table. See the problem extension please. Thanks – fareed Feb 08 '13 at 22:11
  • 1
    Interesting. That's actually caused by ``. The same happens when switching back to `` while keeping ``. The `FacesContext#getRenderResponse()` somehow returns `false` instead. Use `if (context.getCurrentPhaseId == PhaseId.RENDER_RESPONSE)` instead. Or, just check if the desired request parameter is present or not. – BalusC Feb 09 '13 at 00:21
  • Bingo!! that was it. Can you elaborate more about why the FacesContext#getRenderResponse() has returned true on the first view rendering request? thanks a lot this has solved my problem :) – fareed Feb 09 '13 at 23:44
  • I haven't looked closer at it yet. Was you using Mojarra or MyFaces? I observed this issue in Mojarra. – BalusC Feb 10 '13 at 00:25
  • 1
    On a quick test I observed that MyFaces had the same problem. On further inspection I realized that `getRenderResponse()` would only return `true` if `renderResponse()` is **explicitly** been invoked, which wouldn't happen if JSF would just run through every single phase of the lifecycle, as would happen when a `` is present. Right, this was a "d'oh" moment. I think I need to fix my answers regarding returning the `StreamedContent`. – BalusC Feb 11 '13 at 12:15
  • @BalusC 10 years after this answer.. is there a JSF-standard way of doing this? i.e. not using PrimeFaces or OmniFaces? – Edward Apr 11 '23 at 02:40