简体   繁体   中英

How to prevent gwt app to go to login page after page refresh?

I have a gwt app with a login page and a main page. After login app goes to main page. What i want is if i refresh the page to stay in main page and not going to login page. I have read many things and i tried History Mechanish but no result. Here is my code:

@Override
    public void onSuccess(Login result) {

        if (result.getLoginCount() == 1) {

            final VerticalPanel userPanel = new VerticalPanel();

            Anchor logout = new Anchor("logout");
            logout.addStyleName("user");

            logout.addClickHandler(new ClickHandler() {

                @Override
                public void onClick(ClickEvent event) {
                    loginPanel.setVisible(true);
                    tablePanel.setVisible(false);
                    addPanel.setVisible(false);
                    userPanel.setVisible(false);

                }
            });

            Label user = new Label("Hi " + usernameBox.getText());

            userPanel.add(user);
            user.addStyleName("user");
            userPanel.add(logout);
            userPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
            userPanel.setVisible(true);

            usernameBox.setText("");
            passwordBox.setText("");

            RootPanel.get("user").add(userPanel);

            loginPanel.setVisible(false);
            tablePanel.setVisible(true);
            addPanel.setVisible(true);

            History.newItem("main");

            History.addValueChangeHandler(new ValueChangeHandler<String>() {

                @Override
                public void onValueChange(ValueChangeEvent<String> event) {

                    if(History.getToken().equals("main")){

                        loginPanel.setVisible(false);
                        tablePanel.setVisible(true);
                        addPanel.setVisible(true);

                    }

                }
            });

        }

i also tried:

String historyToken = event.getValue();

                    if(historyToken.substring(0 , 4).equals("main")){

                        loginPanel.setVisible(false);
                        tablePanel.setVisible(true);
                        addPanel.setVisible(true);
                    } else {
                        loginPanel.setVisible(true);
                        tablePanel.setVisible(false);
                        addPanel.setVisible(false);
                    }

Is this the right way to handle page refresh with History.addValueChangeHandler? I would appreciate any help.

GWT application is a single page application. It means that if your reload page, the state of your application will be lost. What you can do, is to use local storage to store same state data, but that is not a good idea for an authentication data.

I recommend you to refactor your code in a way that the authentication is done against the back end and your GWT client will recover it's state from back end data when user refreshes the page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM