GWTP显示应用程序启动时的默认位置,即使url说要去其他地方

时间:2016-03-17 13:11:51

标签: gwt gwtp gwt-platform

我想知道如何改变gwtp行为。 当我启动gwt app(在浏览器中输入app url)时,它始终显示给我default place。但是当我按以下方式输入网址时:localhost/app#settings gwtp应该打开Settings位置,但不幸的是它会显示Default个地方。

网络浏览器地址中的网址指向Settings,但视图来自default位置。

我想gwtp为我显示来自网址的视图。

这是我的配置:

public class UiModule extends AbstractGinModule {
    @Override
    protected void configure() {

    bind(AppView.Binder.class).in(Singleton.class);
    bind(Footer.Binder.class).in(Singleton.class);
    bind(GatekeeperProtectedMenuPanel.Binder.class).in(Singleton.class);



    install(new GinFactoryModuleBuilder().build(MenuEntryFactory.class));

    }
public class ClientModule extends AbstractPresenterModule {
    @Override
    protected void configure() {
    bind(RestyGwtConfig.class).asEagerSingleton();

    install(new DefaultModule.Builder()//
        .defaultPlace(Routing.HOME.url)//
        .errorPlace(Routing.ERROR.url)//
        .unauthorizedPlace(Routing.LOGIN.url)//
        .tokenFormatter(RouteTokenFormatter.class).build());
    install(new AppModule());
    install(new GinFactoryModuleBuilder().build(AssistedInjectionFactory.class));

    bind(CurrentUser.class).in(Singleton.class);
    bind(IsAdminGatekeeper.class).in(Singleton.class);
    bind(UserLoginGatekeeper.class).in(Singleton.class);



    // Load and inject CSS resources
    bind(ResourceLoader.class).asEagerSingleton();

    }

}
public class AppModule extends AbstractPresenterModule {
    @Override
    protected void configure() {
    install(new UiModule());

    // Application Presenters

    bindPresenter(AppPresenter.class, AppPresenter.MyView.class, AppView.class, AppPresenter.MyProxy.class);
    bindPresenter(HomePresenter.class, HomePresenter.MyView.class, HomeView.class, HomePresenter.MyProxy.class);
    bindPresenter(ErrorPresenter.class, ErrorPresenter.MyView.class, ErrorView.class, ErrorPresenter.MyProxy.class);
    bindPresenter(TestPresenter.class, TestPresenter.MyView.class, TestView.class, TestPresenter.MyProxy.class);
    bindPresenter(PagePresenter.class, PagePresenter.MyView.class, PageView.class, PagePresenter.MyProxy.class);
    bindPresenter(SettingsPresenter.class, SettingsPresenter.MyView.class, SettingsView.class, SettingsPresenter.MyProxy.class);
    bindPresenter(FilesPresenter.class, FilesPresenter.MyView.class, FilesView.class, FilesPresenter.MyProxy.class);
    bindPresenter(AdminAreaPresenter.class, AdminAreaPresenter.MyView.class, AdminAreaView.class, AdminAreaPresenter.MyProxy.class);

    bindPresenter(LoginPresenter.class, LoginPresenter.MyView.class, LoginView.class, LoginPresenter.MyProxy.class);
    }
}

当我在地方的GateKeeper主持人时会发生这种情况。

这是代码:

public class UserLoginGatekeeper extends UserLoginModel implements Gatekeeper {
    private final CurrentUser currentUser;

    @Inject
    UserLoginGatekeeper(CurrentUser currentUser) {
        this.currentUser = currentUser;
    }

    @Override
    public boolean canReveal() {
        return currentUser.isLoggedIn();
    }
}

在我的主应用程序演示者中,我执行异步调用服务器以检查用户是否登录。如果是这样,我设置客户端变量currentUser.setLoggedIn(true);。基于此Gatekeeper允许访问应用的受限制部分。

我认为问题在于我的异步电话被触发迟到了。并且GWTP重定向到默认位置。

这是我的应用演示者代码:

public class AppPresenter extends TabContainerPresenter<AppPresenter.MyView, AppPresenter.MyProxy> implements AppUiHandlers, CurrentUserChangedHandler, AsyncCallStartHandler, AsyncCallFailHandler,
    AsyncCallSucceedHandler {

    @ProxyStandard
    public interface MyProxy extends Proxy<AppPresenter> {
    }


    public interface MyView extends TabView, HasUiHandlers<AppUiHandlers> {
    void refreshTabs();

    void setTopMessage(String string);

    void setLoginButtonVisbility(boolean isVisible);
    }


    @RequestTabs
    public static final Type<RequestTabsHandler> SLOT_REQUEST_TABS = new Type<>();

    @ChangeTab
    public static final Type<ChangeTabHandler> SLOT_CHANGE_TAB = new Type<>();


    public static final NestedSlot SLOT_TAB_CONTENT = new NestedSlot();

    private static final LoginService service = GWT.create(LoginService.class);
    private final PlaceManager placeManager;
    private final CurrentUser currentUser;

    @Inject
    AppPresenter(EventBus eventBus, MyView view, MyProxy proxy, PlaceManager placeManager, CurrentUser currentUser) {
    super(eventBus, view, proxy, SLOT_TAB_CONTENT, SLOT_REQUEST_TABS, SLOT_CHANGE_TAB, RevealType.Root);
    this.placeManager = placeManager;
    this.currentUser = currentUser;
    getView().setUiHandlers(this);
    onStart();
    }

    protected void onStart() {
    service.isCurrentUserLoggedIn(new MethodCallback<Boolean>() {
        @Override
        public void onFailure(Method method, Throwable exception) {
        MaterialToast.fireToast("Fail to check is current user logged in " + method + " " + exception.getLocalizedMessage());
        }

        @Override
        public void onSuccess(Method method, Boolean response) {
        currentUser.setLoggedIn(response);
        getView().setLoginButtonVisbility(response);
        }
    });
    };

    @ProxyEvent
    @Override
    public void onCurrentUserChanged(CurrentUserChangedEvent event) {
    getView().refreshTabs();
    }

    @ProxyEvent
    @Override
    public void onAsyncCallStart(AsyncCallStartEvent event) {
    getView().setTopMessage("Loading...");
    }

    @ProxyEvent
    @Override
    public void onAsyncCallFail(AsyncCallFailEvent event) {
    getView().setTopMessage("Oops, something went wrong...");
    }

    @ProxyEvent
    @Override
    public void onAsyncCallSucceed(AsyncCallSucceedEvent event) {
    getView().setTopMessage(null);
    }


    @Override
    public void onLogoutButtonClick() {
    service.logout(new MethodCallback<Void>() {

        @Override
        public void onFailure(Method method, Throwable exception) {
        MaterialToast.fireToast("Fail to logout " + method + " " + exception.getLocalizedMessage());
        }

        @Override
        public void onSuccess(Method method, Void response) {
        MaterialToast.fireToast("You have been Succefully logout");
        PlaceRequest request = new PlaceRequest.Builder(placeManager.getCurrentPlaceRequest()).nameToken(Routing.Url.login).build();
        placeManager.revealPlace(request);
        currentUser.setLoggedIn(false);
        getView().setLoginButtonVisbility(false);
        }

    });

    }
}

工作解决方案:

/**
 * 
 */
package pl.korbeldaniel.cms.client.gin;

import gwt.material.design.client.ui.MaterialToast;

import org.fusesource.restygwt.client.Method;
import org.fusesource.restygwt.client.MethodCallback;

import pl.korbeldaniel.cms.client.place.Routing;
import pl.korbeldaniel.cms.client.security.CurrentUser;
import pl.korbeldaniel.cms.client.service.LoginService;

import com.google.gwt.core.shared.GWT;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.Bootstrapper;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.shared.proxy.PlaceRequest;

/**
 * @author korbeldaniel
 *
 */
public class MyBootstrapper implements Bootstrapper {
    private final PlaceManager placeManager;
    private final CurrentUser currentUser;
    private static final LoginService service = GWT.create(LoginService.class);

    @Inject
    public MyBootstrapper(PlaceManager placeManager, CurrentUser currentUser) {
    this.placeManager = placeManager;
    this.currentUser = currentUser;
    }

    @Override
    public void onBootstrap() {
    GWT.log("OnBootstrap");
    service.isCurrentUserLoggedIn(new MethodCallback<Boolean>() {
        @Override
        public void onFailure(Method method, Throwable exception) {
        MaterialToast.fireToast("Fail to check is current user logged in " + method + " " + exception.getLocalizedMessage());
        placeManager.revealErrorPlace("Fail to check is current user logged in " + method + " " + exception.getLocalizedMessage());
        }

        @Override
        public void onSuccess(Method method, Boolean response) {
        // MaterialToast.fireToast("1Current user is logged in: " +
        // response);
        currentUser.setLoggedIn(response);
        if (response == true) {
            placeManager.revealCurrentPlace();
        } else {
            placeManager.revealPlace(new PlaceRequest.Builder().nameToken(Routing.Url.login).build());
        }
        }
    });
    };

}

1 个答案:

答案 0 :(得分:2)

是的,您的后端调用是异步的,很可能UserLoginGatekeeper代码将在后端调用返回并且用户被重定向到默认页面之前运行。

有两种解决方案:

  1. 使用动态生成的主页(index.html)并通过后端将javascript变量设置为用户详细信息。您可以在自定义Bootstraper实施中读出用户详细信息,并设置CurrentUser

  2. 如果您不想使用动态生成的主页,您还可以将后端调用isCurrentUserLoggedIn移至自定义Bootstrapper实施中,并移至{{1}回调显示第一页(如上面链接的GWTP文档中所述)