1

I am building a CRUD web application using JSF. I have a problem with loading a page after the user session has timed out. That is i lose the parameters I need to construct the view (even though the parameters are still visible in the url like so: 'someurl/view.xhtml?pid=5'.

In the .xhtml file the parameter pid is used to load some content from an underlying database when constructing the view. When the user has been inactive for a while their session times out, and if they try to reload the page in the browser they are forwarded to the login page (the 'someurl/view.xhtml?pid=5' still intact) and on succesful login go back to the view.xhtml page where I wan't the view to be constructed as if their session had never timedout.

However this does not happen because the 'pid' parameter is no longer set in the view. But since the 'pid' parameter is still visible in the url I feel like I should be able to get it into the view and load the protein with this id from the database.

These are the things I've tried:

    #{protein.setProteinById(param.pid)}

and

    #{protein.setProteinById(param['pid'])}

and

    #{protein.setProteinById(request.getParameter('pid'))}

and

    <c:set value="${request.getParameter('pid')}" var="pid" />
    #{protein.setProteinById(pid)}

Is this possible to do? Then how?

numfar
  • 1,637
  • 4
  • 18
  • 39

1 Answers1

0

I'm no expert, but wouldn't you set it in the managed bean?

As far as I know there are two methods for doing this.

  1. One method is using in your facelet to push a view parameter back into a bean (I have a scenario where this doesn't work because of other things, so have no experience with it)

    What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?

  2. I have a prerender method, which is always called before a render

<f:metadata>
  <f:event type="preRenderView" listener="#{MyController.prerenderMethod}" />
</f:metadata>

And inside the method, I look at the parameters:

public void prerender(ComponentSystemEvent event) {
    value = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("key");

}
Community
  • 1
  • 1
Niels Bech Nielsen
  • 4,777
  • 1
  • 21
  • 44
  • Thanks for the tip. I tried `FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("pid")` in a listner method called as specified above. And printed its value. This gives me null. So it doesn't fix my problem. Is the parameter not available somehow even though it is visible in the address field? – numfar Mar 05 '14 at 14:49
  • Perhaps you could tell what version of JSF you are using. I'm on JSF 2.2 (2.2.5 actually), and that is how I read my args. – Niels Bech Nielsen Mar 05 '14 at 14:53
  • Just created that basic example with a ManagedBean RequestScoped, and it worked just fine locally. Probably version dependent, than. – Niels Bech Nielsen Mar 05 '14 at 15:05
  • I have JSF 2.2. I just discovered something weird though. I wanted to see what was in the map returned by `getRequestParameterMap()‌​` So added this line `System.out.println("Piddy "+FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap());` after this one: `String pid = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("pid");`. And all of a sudden it works! – numfar Mar 05 '14 at 15:08
  • And removing the line that prints the map gives med null again. – numfar Mar 05 '14 at 15:09
  • Hrm. save, compile, redeploy? :) – Niels Bech Nielsen Mar 05 '14 at 15:10
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/49072/discussion-between-niels-bech-nielsen-and-numfar) – Niels Bech Nielsen Mar 05 '14 at 15:11