Im currently working on Spring Application and found it quite good stuff, but i faced a problem and can’t solve it.
I have Navigator with SpringViewProvider binded and UI class is also @PreserveOnRefresh. Currently viewProvider contains 3 views (“”, “login”, “viewA”).
After successful navigation on “login” view, i’m able to press back button and return to “login” view again(which is not good). I tried so far on “viewA” enter use
Page.getCurrent().setUriFragment(uri, false);
And this didn’t worked out.
How to solve this problem?
@Olli Yes, I am. If i hit the view that doesnt specified in a VIEW_TOKENS, then it wont navigate to that view and uriFragment is changed to prvious state. For example:
case 1:
Initially is given: http://localhost:8090/#!signin
what trying to do: change ‘signin’ on ‘viewthatdoesntspecified’
Result: View change doesn’t happen, uri remains as it was ( see ‘Initially’)
case 2:
Initially is given: http://localhost:8090/#!signin
what trying to do: change ‘signin’ on ‘viewA’
Result: View changes accordingly,but on back button press, it returns to previous state (at this point: ‘signin’)
To sum up, my point is to make case 1 work everytime, also include case 2 working properly
public boolean beforeViewChange(ViewChangeEvent event) {
if(ViewTokens.AVAILABLE_TOKENS.contains(
UI.getCurrent().getNavigator().getState())) {
return true;
}
return false;
}
If I’m understanding your case correctly, you’re trying to navigate somewhere that doesn’t exist in AVAILABLE_TOKENS, so you end up returning false from the beforeViewChange method on line 6, which means that you’re
blocking the view change completely . Instead, you could just set up an Error View with navigator.setErrorView(new ErrorView()) , where the ErrorView can show you either an error page (“view doesn’t exist”) or redirect you to, for example, the login view. For the latter possibility, see below:
case 2:
In your LoginView’s
enter method, check that if you’re already logged in. How you do this depends on how your authentication is set up - a simple way would be to use a cookie. If you’re logged in, just tell navigator to take you to whereever you want to with navigator.navigateTo(…).
@Olli Thank you so much for assist, i finally resolve the problem. i found pretty easy solution besides cookies ( found only partial solution of that) . And yes, about case 2 - I was thinking exactly the same as you did. After few hours i made thing get working!
Sorry for soo ‘fast’ response[quote=Olli Tietäväinen]
As for case 1:
public boolean beforeViewChange(ViewChangeEvent event) {
if(ViewTokens.AVAILABLE_TOKENS.contains(
UI.getCurrent().getNavigator().getState())) {
return true;
}
return false;
}
If I’m understanding your case correctly, you’re trying to navigate somewhere that doesn’t exist in AVAILABLE_TOKENS, so you end up returning false from the beforeViewChange method on line 6, which means that you’re
blocking the view change completely . Instead, you could just set up an Error View with navigator.setErrorView(new ErrorView()) , where the ErrorView can show you either an error page (“view doesn’t exist”) or redirect you to, for example, the login view. For the latter possibility, see below:
case 2:
In your LoginView’s
enter method, check that if you’re already logged in. How you do this depends on how your authentication is set up - a simple way would be to use a cookie. If you’re logged in, just tell navigator to take you to whereever you want to with navigator.navigateTo(…).