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.
Login Help plz.
Hello everyone
I'm trying to make a functional login, I have three views, one is login and 2 windows but with a superior menu to navigate them with Navigator, to understand me better leave my code below, if I'm not logged me should always send the login view, if I have logged incorrectly me to send to the login window, and if I have successfully logged menuOne I should send it my main window.
The problem is that I correctly enter the session and sent to the main window but does not display the menu ... so we appreciate your help to solve this problem, it's the same when you log off or if I .... they could explain what other way I can solve ...
My codes:
Main:
package com.vaadin.ricardo.proyecto.logintest;
import javax.servlet.annotation.WebServlet;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.annotations.Widgetset;
import com.vaadin.navigator.Navigator;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.MenuBar.MenuItem;
import com.vaadin.ui.Panel;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
@Theme("mytheme")
@Widgetset("com.vaadin.ricardo.proyecto.logintest.MyAppWidgetset")
public class MyUI extends UI {
@Override
protected void init(VaadinRequest vaadinRequest) {
VerticalLayout layout = new VerticalLayout();
MenuBar menu = new MenuBar();
menu.setSizeFull();
MenuItem mnuArchivo = menu.addItem("File", null);
MenuItem mnuSalir = mnuArchivo.addItem("Close Sesion", null);
MenuItem mnuModule = menu.addItem("Modules", null);
MenuItem mnuWindowsOne = mnuModule.addItem("Windows One", null);
MenuItem mnuWindowsTwo = mnuModule.addItem("Windows Two", null);
layout.addComponent(menu);
Panel p = new Panel("Modules");
layout.addComponent(p);
setContent(layout);
Navigator nav = null;
if (getSession().getAttribute("access")!=null && getSession().getAttribute("access")!="false") {
System.out.println("VER1:"+getSession().getAttribute("access"));
nav = new Navigator(this, p);
nav.addView("", WindowsOne.class);
nav.addView(LoginView.NAME, LoginView.class);
nav.addView(WindowsOne.NAME, WindowsOne.class);
nav.addView(WindowsTwo.NAME, WindowsTwo.class);
MyUI.getCurrent().getNavigator().navigateTo("menuOne");
} else {
System.out.println("VER:"+getSession().getAttribute("access"));
nav = new Navigator(this, this);
nav.addView("", LoginView.class);
nav.addView(LoginView.NAME, LoginView.class);
nav.addView(WindowsOne.NAME, WindowsOne.class);
nav.addView(WindowsTwo.NAME, WindowsTwo.class);
MyUI.getCurrent().getNavigator().navigateTo("login");
}
mnuSalir.setCommand(new MenuBar.Command() {
@Override
public void menuSelected(MenuBar.MenuItem selectedItem) {
getSession().close();
MyUI.getCurrent().getNavigator().navigateTo("login");
}
});
mnuWindowsOne.setCommand(new MenuBar.Command() {
@Override
public void menuSelected(MenuItem selectedItem) {
MyUI.getCurrent().getNavigator().navigateTo("menuOne");
}
});
mnuWindowsTwo.setCommand(new MenuBar.Command() {
@Override
public void menuSelected(MenuItem selectedItem) {
MyUI.getCurrent().getNavigator().navigateTo("menuTwo");
}
});
}
@WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public static class MyUIServlet extends VaadinServlet {
}
}
LoginView
package com.vaadin.ricardo.proyecto.logintest;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
public class LoginView extends VerticalLayout implements View{
public static final String NAME="login";
FormLayout form=new FormLayout();
public LoginView(){
final TextField txtUsername=new TextField("UserName");
final PasswordField txtPassword=new PasswordField("Password");
form.addComponent(txtUsername);
form.addComponent(txtPassword);
Button btn=new Button("Log In");
this.addComponent(form);
this.addComponent(btn);
btn.addClickListener(new ClickListener() {
@Override
public void buttonClick(Button.ClickEvent event) {
if(txtUsername.getValue().equals("admin") || txtPassword.getValue().equals("admin")){
getSession().setAttribute("access", "true");
MyUI.getCurrent().getNavigator().navigateTo("menuOne");
}else{
getSession().setAttribute("access", "false");
}
}
});
}
@Override
public void enter(ViewChangeListener.ViewChangeEvent event) {
}
}
WindowsOne
package com.vaadin.ricardo.proyecto.logintest;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
public class WindowsOne extends VerticalLayout implements View{
public static final String NAME="menuOne";
public WindowsOne(){
this.addComponent(new Label("THIS IS MY WINDOWS ONE"));
}
@Override
public void enter(ViewChangeListener.ViewChangeEvent event) {
}
}
WindowsTwo
package com.vaadin.ricardo.proyecto.logintest;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
public class WindowsTwo extends VerticalLayout implements View{
public static final String NAME="menuTwo";
public WindowsTwo(){
this.addComponents(new Label("This is my windows two"));
}
@Override
public void enter(ViewChangeListener.ViewChangeEvent event) {
}
}
Greetings to all
I made another change in the main , but while changing the direction not send it , it seems that destroyed or something, attached image.
package com.vaadin.ricardo.proyecto.logintest;
import javax.servlet.annotation.WebServlet;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.annotations.Widgetset;
import com.vaadin.navigator.Navigator;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.MenuBar.MenuItem;
import com.vaadin.ui.Panel;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
@Theme("mytheme")
@Widgetset("com.vaadin.ricardo.proyecto.logintest.MyAppWidgetset")
public class MyUI extends UI {
@Override
protected void init(VaadinRequest vaadinRequest) {
VerticalLayout layout = new VerticalLayout();
MenuBar menu = new MenuBar();
menu.setSizeFull();
MenuItem mnuArchivo = menu.addItem("File", null);
MenuItem mnuSalir = mnuArchivo.addItem("Close Sesion", null);
MenuItem mnuModule = menu.addItem("Modules", null);
MenuItem mnuWindowsOne = mnuModule.addItem("Windows One", null);
MenuItem mnuWindowsTwo = mnuModule.addItem("Windows Two", null);
layout.addComponent(menu);
Panel p = new Panel("Modules");
layout.addComponent(p);
//setContent(layout);
LoginView login=new LoginView();
Navigator nav = new Navigator(this, p);
nav.addView("", WindowsOne.class);
nav.addView(LoginView.NAME, LoginView.class);
nav.addView(WindowsOne.NAME, WindowsOne.class);
nav.addView(WindowsTwo.NAME, WindowsTwo.class);
if (getSession().getAttribute("access")!=null && getSession().getAttribute("access")!="false") {
System.out.println("VER1:"+getSession().getAttribute("access"));
MyUI.getCurrent().getNavigator().navigateTo("menuOne");
setContent(layout);
} else {
System.out.println("VER:"+getSession().getAttribute("access"));
this.setContent(login);
}
mnuSalir.setCommand(new MenuBar.Command() {
@Override
public void menuSelected(MenuBar.MenuItem selectedItem) {
getSession().close();
MyUI.getCurrent().getNavigator().navigateTo("login");
}
});
mnuWindowsOne.setCommand(new MenuBar.Command() {
@Override
public void menuSelected(MenuItem selectedItem) {
MyUI.getCurrent().getNavigator().navigateTo("menuOne");
}
});
mnuWindowsTwo.setCommand(new MenuBar.Command() {
@Override
public void menuSelected(MenuItem selectedItem) {
MyUI.getCurrent().getNavigator().navigateTo("menuTwo");
}
});
}
@WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public static class MyUIServlet extends VaadinServlet {
}
}