I have a simple application with a LoginView class (route “”) and a MainView class (route “main”). This classes implements BeforeEnterObserver where session validation is checked. If session is valid i need to move on “main” from “” and the opposite if session is outdated. I’ve trayed reroute and works but the url is not rewrited…
I see navigate() method from UI and works, the url is rewrited, but this only is executed if associated to component (button click event for exemple)
Is possible to have a small example for this? I did not find anything about this on Google for vaadin 10…
Can someone help me? Thank you
If event.rerouteTo(OtherView.class); (or event.rerouteTo("other-route");) in beforeEnter doesn’t update the URL, I’d consider that a bug. Is that the case?
@Route("")
public class LoginView extends VerticalLayout implements BeforeEnterObserver {
@PersistenceContext
private EntityManager entityManager;
boolean session = true;
public LoginView() {
TextField username = new TextField("Username");
PasswordField password = new PasswordField("Password");
Button loginButton = new Button("Login");
loginButton.addClickListener((ComponentEventListener<ClickEvent<Button>>)
buttonClickEvent -> doLogin(username.getValue(), password.getValue()));
loginButton.getElement().setAttribute("theme", "primary");
HorizontalLayout buttonsLayout = new HorizontalLayout(loginButton);
VerticalLayout divLayout = new VerticalLayout(username, password, buttonsLayout);
divLayout.setAlignSelf(Alignment.END, buttonsLayout);
Div loginDiv = new Div(divLayout);
setAlignItems(Alignment.CENTER);
username.focus();
add(loginDiv);
}
@Override
public void beforeEnter(BeforeEnterEvent beforeEnterEvent) {
if (session == true) {
beforeEnterEvent.rerouteTo(MainView.class);
}
}
....
}
And this MainView class:
@Route("main")
public class MainView extends VerticalLayout {
public MainView() {
Label label = new Label("main");
add(label);
}
}
After started application if i browse http://127.0.0.1/ i see “main” label (the MainView class is loaded) but the URL is not http://127.0.0.1/main. It’s not changed! My framework version is 10.0.0.beta10.