0

My managed bean used to be @ViewScoped and I used a @PostConstruct annotated method to situate various functions that I wanted performed each time the page is refreshed via the browser URL. However, the page requires query string parameter processing and I needed to use some AJAX and then redirect to the same page after the AJAX so, to avoid reconcatenating the query string params from the cache into the URL string, I changed the scope to @SessionScoped so that I can just do

FacesContext.getCurrentInstance().getExternalContext().redirect("mypage.jsf");

and the session parameters that would need to be reloaded from the query string if it were @ViewScoped could just stay there. However, the problem is that the @PostConstruct method, among other things that are OK to be done only at the start of a session, does make an EJB call that should be executed on each page reload, regardless of the scope. For form submissions it is easy because I just call that EJB method in the managed bean method that the form submission calls but the problem is that it doesn't get run if the user hits refresh in the browser like it did in @ViewScoped.

Is there any way for me to keep the best of both scopes, IOW to retain the already processed query string params in the cache without having to reprocess them, like @SessionScoped affords me to while at the same time have a method that is invoked on every page reload and not just when the managed bean lifecycle begins?

I understand I could go back to @ViewScoped and reconcatenate all the query string params after mypage.jsf in the code sample above but that seems too manual. I was hoping there was a shortcut in JSF.

amphibient
  • 29,770
  • 54
  • 146
  • 240
  • I haven't understood your question so well, I mean, when you use Ajax, if you send a redirection response, you'll get redirected to a new view (url address). That collides with the goal of using Ajax, which is to update DOM tree parts without having to reload the whole view. So, if going to perform a redirection, then why to use Ajax? Just perform a plain GET request to the same view, you don't need to include all the query params you already have again, just use [`includeViewParams=true`](http://stackoverflow.com/a/10353792/1199132). – Aritz Jul 01 '15 at 20:45
  • that part is almost outside the scope of the question. i need to use AJAX because i like how form data validation works that way (rather than reloading) but once the form has been validated positively, i do a redirect because something else in the page (that is too complicated to explain) works better that way – amphibient Jul 01 '15 at 20:50
  • You mean you want to validate only some form inputs when sending it in an action and other ones in other action? You can customize what to validate depending on the action, using request params. I think you consider that part to be outside the scope of the question, but in fact is the root for your hacky workaround isn't it? Anyway, the answer for your question title is that you need to invoke it in the view, calling the session scoped bean method specifically. Let's say `#{sessionBean.reloadMyPage}` – Aritz Jul 01 '15 at 21:08
  • like i said, that is almost outside the scope, i am almost regretting now including it because what really matters is that the bean needs to be session scoped AND it needs to call a method on each reload. i would focus on those two requirements and ignore the AJAX, if i were you – amphibient Jul 01 '15 at 21:20

0 Answers0