1

what's the best practice for navigating back many times (redirecting back)? I created a sessionScoped Bean with a stack but this spoils the navigation in case of having opened different tabs pushing wrong urls.

Navigation Example:

Page A -> Page B -> Page C

Page C -> Page B -> A

How to get the last url and get back and get back again? Ok I implemented viewParams now still don't know to navigate back.

MrCodex
  • 15
  • 4
  • Use `ViewScoped` to support multiple tabs. See [How to choose the right bean scope?](http://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean-scope) – Jasper de Vries Apr 24 '17 at 09:06
  • @JasperdeVries How does ViewScoped will help me in navigating back many times? – MrCodex Apr 24 '17 at 09:11
  • That part of your question is too broad / unclear. – Jasper de Vries Apr 24 '17 at 09:14
  • @JasperdeVries I just wanted to know what's the best pratice for navigating back. I used a stack for this solution but side effects are parallel opened tabs. If I navigate in one tab the stack changes and navigation back in another tab will change will end up in faulty back urls. – MrCodex Apr 24 '17 at 09:16

1 Answers1

1

The best model for handling navigation is to rely on the client browser remembering which URLs it has been to; this is the case for all frameworks not just JSF.

JSF makes it easy (and tempting) to maintain a lot of unnecessary state on the session. Keep your session scoped beans as light as possible and make sure that everything that is needed to properly initialise the web beans is encoded in the URL within view parameters. That way you don't have to re-invent the wheel and everything will work without surprises, regardless of how many tabs the client has open.

StuPointerException
  • 7,117
  • 5
  • 29
  • 54
  • Is it a bad practice to use SessionStorage for this keeping up an array with urls and push and popping according to navigation? – MrCodex Apr 25 '17 at 07:00
  • I think it's unnecessary, and certainly open to errors (as you're discovering). It would just be duplicating what the client browser is already doing. – StuPointerException Apr 25 '17 at 07:54
  • I pass parameters through url adding Params. What's the best practice? You said that I shouldn't use sessionStorage... Can you give me a cleaner hint please? – MrCodex Apr 25 '17 at 08:18
  • And If I redirect my viewParams are null... Therefor I put the parameters directly into the url – MrCodex Apr 25 '17 at 08:20
  • Can you give me any detailed hints please? :( – MrCodex Apr 25 '17 at 08:52
  • The pattern I use is to add the properties to the page as `viewParams` which map to properties on your web beans. When you redirect it's simply a case of adding the query string to the URL so the view params are available on the next page. If you are having problems getting this to work I would suggest you ask a new question explaining what isn't working. Good luck! – StuPointerException Apr 25 '17 at 08:58
  • Yeah got it. I just link according to existing viewParams. Thank you very much. – MrCodex Apr 28 '17 at 06:38