Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
Vaadin Spring - Prevent to navigate back
Hello everyone,
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?
You can control access to views by using a ViewChangeListener:
https://vaadin.com/wiki/-/wiki/Main/Access+control+for+views
Note that instead of showing an error, you can also just use navigator.navigateTo to navigate to some other view than your login view.
-Olli
Hello,
@Tobias Demuth, i forgot to mention that i already use ViewChangeListener. My Bad.
VIEW_TOKENS:
String HOME = "";
String SIGNIN = "login";
String VIEWA = "viewA";
List<String> AVAILABLE_TOKENS = Arrays.asList(
HOME,
SIGNIN,
VIEWA
);
ViewListener:
public class MyViewListener implements ViewChangeListener {
@Override
public boolean beforeViewChange(ViewChangeEvent event) {
if(ViewTokens.AVAILABLE_TOKENS.contains(
UI.getCurrent().getNavigator().getState())) {
return true;
}
return false;
}
@Override
public void afterViewChange(ViewChangeEvent event) {
}
}
@Olli your suggestion i've tried in a first place, but for a some reason it won't work at all. As i said i tried also like this idea:
if(getUI().getNavigator().getState() == ViewToken.ViewA) {
Page.getCurrent().setUriFragment(ViewToken.SIGNIN, false);
}
But it failed.
Can you debug it a little: Are you hitting the beforeViewChange/afterViewChange methods when you navigate using the back button at all?
-Olli
@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
I assume, i got your question correctly
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(...).
-Olli
@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
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(...).-Olli