为什么我的History.newItem(someToken)不会触发onValueChange()?

时间:2011-12-17 18:53:32

标签: gwt token browser-history gwt-2.4

即使在我使用History.fireCurrentHistoryState();

时正确触发了它

编辑:同一个包中的所有类。代码已更新 -

TestHistory.java

public class TestHistory implements EntryPoint, ValueChangeHandler<String> {

    static boolean isLoggedIn = false;
    static final String PAGENAME = "mainscreen";
    public void onModuleLoad()
    {
        History.addValueChangeHandler(this);

        String startToken = History.getToken();
        System.out.println("onModuleLoad Called..... start token= -------"+startToken+"--------");
        if(!startToken.isEmpty())
            History.newItem(startToken);
        History.fireCurrentHistoryState(); //to execute onValueChange 1st time since 1st time history is not setup
    }

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

        String token = event.getValue();

        String args = "";
        int question = token.indexOf("?");
        if (question != -1) {
        args = token.substring(question + 1);
        token = token.substring(0, question);
        }

        if(!isLoggedIn)
        {
            if(token.isEmpty() || "login".equals(token))    //1st time opened the site normally
                new Login().display(false, RootPanel.get());
            else {
                new Login().display(true, RootPanel.get());
            }
        }
        else    //User has logged in
        {
            if(token.isEmpty() || "login".equals(token))
            {
                if(isLoggedIn)
                    Window.alert("Ur already logged in!!!");
                else
                    new Login().display(false, RootPanel.get());
            }
            else if("withdraw".equals(token))
                new Withdraw().display(RootPanel.get(), args);
            else if("deposit".equals(token))
                new Deposit().display(RootPanel.get(), args);
            else //token not clear
                Window.alert("Unrecognized token=" + token);
        }   

    }

}

Login.java

public class Login {
    static final String PAGENAME = "login";
    void display(final boolean hasTypedSomeToken, Panel myPanel) //Process login
    {
        System.out.println("login display called");
        Label displayLabel = new Label("This is the Login Page");
        Label enterName = new Label("Enter ur name");
        final TextBox txtName = new TextBox();
        Label enterPasswd = new Label("Enter ur Passwd");
        final TextBox txtPasswd = new TextBox();
        Button btnLogIn = new Button("Login", new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {

                /* Real app will check DB. Here we r jst chckng d txt fields hv value */
                if(txtName.getValue().length()>0 && txtPasswd.getValue().length()>0)
                {
                    TestHistory.isLoggedIn = true;
                    if(hasTypedSomeToken) {
                        //History.back(); //send him to the URL(token) he bookmarked b4 loggin in
                        History.newItem("login",false);
                        History.back();
                        System.out.println(History.getToken());
                    }
                    else{
                        myPanel.clear();
                                            Label displayLabel = new Label("Thank U for logging.);
                                            myPanel.add(displayLabel);
                                         }

                }   
            }
        });         
        myPanel.clear();
        myPanel.add(displayLabel);
        myPanel.add(enterName);
        myPanel.add(txtName);
        myPanel.add(enterPasswd);
        myPanel.add(txtPasswd);
        myPanel.add(btnLogIn);
    }
}

Deposit.java

public class Deposit {
    static final String PAGENAME = "deposit";
    void display(Panel myPanel, String param)
    {
        System.out.println("deposit display called");
        myPanel.clear();
        Label displayLabel = new Label("This is the Deposit Page & ur parameter = "+param+")");
        myPanel.add(displayLabel);
    }   
}

Withdraw.java

//类似于deposit.java

1 个答案:

答案 0 :(得分:1)

虽然newItem(...)通常会触发一个事件,但如果当前令牌与您尝试添加的令牌相同,则它是无操作。如果,那么您的实施就会出现问题。