GWTP。如何从一个演示者访问另一个演示者-他们不通过插槽机制连接
我有一个演示者用于登录屏幕。我有整个应用程序的AppPresenter。每个视图(登录除外)都显示在AppView插槽中 在AppView中,我有一个注销按钮,它应该只有在用户登录时才可见,所以当用户登录时,该按钮将变为可见。当用户点击它注销时,按钮应该消失 隐藏它很简单,因为它发生在一个视图/演示者中。但我在用户登录后显示此按钮时遇到问题,因为它发生在其他演示者/视图上 我是gwtp新手,请帮助我解决此问题 我应该如何从登录名访问其他演示者?他们没有关系!如果我将GWTP。如何从一个演示者访问另一个演示者-他们不通过插槽机制连接,gwt,gwtp,gwt-platform,presenter,Gwt,Gwtp,Gwt Platform,Presenter,我有一个演示者用于登录屏幕。我有整个应用程序的AppPresenter。每个视图(登录除外)都显示在AppView插槽中 在AppView中,我有一个注销按钮,它应该只有在用户登录时才可见,所以当用户登录时,该按钮将变为可见。当用户点击它注销时,按钮应该消失 隐藏它很简单,因为它发生在一个视图/演示者中。但我在用户登录后显示此按钮时遇到问题,因为它发生在其他演示者/视图上 我是gwtp新手,请帮助我解决此问题 我应该如何从登录名访问其他演示者?他们没有关系!如果我将AppPresenter类添加
AppPresenter
类添加到LoginPresenter
构造函数中,并使用@Inject
注释,这是否足够好?详情如下:
@Inject
LoginPresenter(EventBus eventBus, MyView view, MyProxy proxy, CurrentUser currentUser, PlaceManager placeManager, AppPresenter appPresenter) {
super(eventBus, view, proxy, RevealType.Root);
this.currentUser = currentUser;
if (!currentUser.isLoggedIn()) {
placeManager.revealDefaultPlace();
}
this.placeManager = placeManager;
this.appPresenter = appPresenter;
getView().setUiHandlers(this);
Window.setTitle(Routing.Name.login);
editorDriver = getView().createEditorDriver();
editorDriver.edit(model);
}
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(AdminAreaPresenter.class, AdminAreaPresenter.MyView.class, AdminAreaView.class, AdminAreaPresenter.MyProxy.class);
bindPresenter(LoginPresenter.class, LoginPresenter.MyView.class, LoginView.class, LoginPresenter.MyProxy.class);
}
}
演讲者:
package pl.daniel.cms.client.place.app;
import gwt.material.design.client.ui.MaterialToast;
import org.fusesource.restygwt.client.Method;
import org.fusesource.restygwt.client.MethodCallback;
import pl.daniel.cms.client.place.Routing;
import pl.daniel.cms.client.security.CurrentUser;
import pl.daniel.cms.client.security.CurrentUserChangedEvent;
import pl.daniel.cms.client.security.CurrentUserChangedHandler;
import pl.daniel.cms.client.service.LoginService;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.gwtplatform.mvp.client.ChangeTabHandler;
import com.gwtplatform.mvp.client.HasUiHandlers;
import com.gwtplatform.mvp.client.RequestTabsHandler;
import com.gwtplatform.mvp.client.TabContainerPresenter;
import com.gwtplatform.mvp.client.TabView;
import com.gwtplatform.mvp.client.annotations.ChangeTab;
import com.gwtplatform.mvp.client.annotations.ProxyEvent;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.annotations.RequestTabs;
import com.gwtplatform.mvp.client.presenter.slots.NestedSlot;
import com.gwtplatform.mvp.client.proxy.AsyncCallFailEvent;
import com.gwtplatform.mvp.client.proxy.AsyncCallFailHandler;
import com.gwtplatform.mvp.client.proxy.AsyncCallStartEvent;
import com.gwtplatform.mvp.client.proxy.AsyncCallStartHandler;
import com.gwtplatform.mvp.client.proxy.AsyncCallSucceedEvent;
import com.gwtplatform.mvp.client.proxy.AsyncCallSucceedHandler;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.Proxy;
import com.gwtplatform.mvp.shared.proxy.PlaceRequest;
/**
* The main {@link com.gwtplatform.mvp.client.Presenter} of the application. It
* contains a number of tabs allowing access to the various parts of the
* application. Tabs are refreshed whenever the current user's privileges change
* in order to hide areas that cannot be accessed.
*/
public class AppPresenter extends TabContainerPresenter<AppPresenter.MyView, AppPresenter.MyProxy> implements AppUiHandlers, CurrentUserChangedHandler, AsyncCallStartHandler, AsyncCallFailHandler,
AsyncCallSucceedHandler {
/**
* {@link AppPresenter}'s proxy.
*/
@ProxyStandard
public interface MyProxy extends Proxy<AppPresenter> {
}
/**
* {@link AppPresenter}'s view.
*/
public interface MyView extends TabView, HasUiHandlers<AppUiHandlers> {
void refreshTabs();
void setTopMessage(String string);
void setLoginButtonVisbility(boolean isVisible);
}
/**
* This will be the event sent to our "unknown" child presenters, in order
* for them to register their tabs.
*/
@RequestTabs
public static final Type<RequestTabsHandler> SLOT_REQUEST_TABS = new Type<>();
/**
* Fired by child proxie's when their tab content is changed.
*/
@ChangeTab
public static final Type<ChangeTabHandler> SLOT_CHANGE_TAB = new Type<>();
/**
* Use this in leaf presenters, inside their {@link #revealInParent} method.
*/
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) {
GWT.log(response.toString());
// MaterialToast.fireToast("User login status: " + method + " "
// + response.toString());
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);
}
/*
* (non-Javadoc)
*
* @see
* pl.daniel.cms.client.place.app.AppUiHandlers#onLogoutButtonClick()
*/
@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("Succefully logout " + method + " " + response);
PlaceRequest request = new PlaceRequest.Builder(placeManager.getCurrentPlaceRequest()).nameToken(Routing.Url.login).build();
placeManager.revealPlace(request);
currentUser.setLoggedIn(false);
getView().setLoginButtonVisbility(false);
}
});
}
}
您完全正确:在构造函数中或字段顶部使用@Inject是一种方法。请确保AppPresenter绑定为singleton
public class ApplicationModule extends AbstractPresenterModule {
@Override
protected void configure() {
bindSingletonPresenterWidget(AppPresenter.class, AppPresenter.MyView.class, AppView.class);
}
}
这样,每次@Inject AppPresenter anywhere时,您都会引用所需的确切实例,是的,这是使用松散绑定的组件和依赖项注入的好处之一。这样做之后,我得到的是没有错误的空白白页。您的GIN注入可能会导致这种情况,我最好的猜测是尝试用maven重新构建它,以便在预编译时看到错误(在superdev中,您不会得到任何错误)。确保没有循环依赖项,并且所有绑定都正确。是。你有交叉参考资料吗?像登录时注入的AppPresenter和登录时注入的AppPresenter?否。请查看我的帖子。我已经更新了presenters代码.bindPresenter(AppPresenter.class,AppPresenter.MyView.class,AppView.class,AppPresenter.MyProxy.class);-这是错误的。通过将其注入LoginPresenter的构造函数,每次都会得到一个新实例,而不是您已经验证过的实例。将其更改为bindSingletonPresenterWidget
public class AppModule extends AbstractPresenterModule {
@Override
protected void configure() {
install(new UiModule());
// Application Presenters
bindSingletonPresenterWidget(AppPresenter.class, AppPresenter.MyView.class, AppView.class);
// bindPresenter(AppPresenter.class, AppPresenter.MyView.class, AppView.class, AppPresenter.MyProxy.class);
}
}
[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building cms 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ cms ---
[INFO] Deleting /home/korbeldaniel/git/cms/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ cms ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 18 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.3:compile (default-compile) @ cms ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 117 source files to /home/korbeldaniel/git/cms/target/cms/WEB-INF/classes
[INFO] /home/korbeldaniel/git/cms/src/main/java/pl/korbeldaniel/cms/server/entity/GenericEntity.java: Some input files use unchecked or unsafe operations.
[INFO] /home/korbeldaniel/git/cms/src/main/java/pl/korbeldaniel/cms/server/entity/GenericEntity.java: Recompile with -Xlint:unchecked for details.
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ cms ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/korbeldaniel/git/cms/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.3:testCompile (default-testCompile) @ cms ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ cms ---
[INFO] No tests to run.
[INFO]
[INFO] --- gwt-maven-plugin:2.7.0:compile (default) @ cms ---
[INFO] auto discovered modules [pl.korbeldaniel.cms.cms]
[INFO] Compiling module pl.korbeldaniel.cms.cms
[INFO] Ignored 1 unit with compilation errors in first pass.
[INFO] Compile with -strict or with -logLevel set to TRACE or DEBUG to see all errors.
[INFO] Computing all possible rebind results for 'pl.korbeldaniel.cms.client.service.LoginService'
[INFO] Rebinding pl.korbeldaniel.cms.client.service.LoginService
[INFO] Invoking generator org.fusesource.restygwt.rebind.RestServiceGenerator
[INFO] Generating: pl.korbeldaniel.cms.client.service.LoginService_Generated_RestServiceProxy_
[INFO] classname to resolve: org.fusesource.restygwt.rebind.ModelChangeAnnotationResolver
[INFO] add annotationresolver: org.fusesource.restygwt.rebind.ModelChangeAnnotationResolver
[ERROR] log4j:WARN No appenders could be found for logger (org.hibernate.validator.util.Version).
[ERROR] log4j:WARN Please initialize the log4j system properly.
[ERROR] log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
[INFO] Compiling 1 permutation
[INFO] Compiling permutation 0...
[INFO] Compile of permutations succeeded
[INFO] Compilation succeeded -- 16,905s
[INFO] Linking into /home/korbeldaniel/git/cms/target/cms/cms
[INFO] Invoking Linker Verify the availability of a more recent version of GWTP.
[INFO] Checking version information for gwtp-mvp-client
[INFO] [WARN] ------------------------------------------------------------
[INFO] [WARN] A new version of gwtp-mvp-client is available!
[INFO] [WARN] Your version: 1.5
[INFO] [WARN] Latest version: 1.5.2
[INFO] [WARN] See http://search.maven.org/#artifactdetails|com.gwtplatform|gwtp-mvp-client|1.5.2|jar
[INFO] [WARN] ------------------------------------------------------------
[INFO] Link succeeded
[INFO] Linking succeeded -- 0,745s
[INFO]
[INFO] --- maven-war-plugin:2.6:war (default-war) @ cms ---
[INFO] Packaging webapp
[INFO] Assembling webapp [cms] in [/home/korbeldaniel/git/cms/target/cms]
[INFO] Processing war project
[INFO] Copying webapp resources [/home/korbeldaniel/git/cms/src/main/webapp]
[INFO] Webapp assembled in [111 msecs]
[INFO] Building war: /home/korbeldaniel/git/cms/target/cms.war
[INFO]
[INFO] --- gwt-maven-plugin:2.7.0:test (default) @ cms ---
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ cms ---
[INFO] Installing /home/korbeldaniel/git/cms/target/cms.war to /home/korbeldaniel/.m2/repository/pl/korbeldaniel/cms/cms/0.0.1-SNAPSHOT/cms-0.0.1-SNAPSHOT.war
[INFO] Installing /home/korbeldaniel/git/cms/pom.xml to /home/korbeldaniel/.m2/repository/pl/korbeldaniel/cms/cms/0.0.1-SNAPSHOT/cms-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 24.520 s
[INFO] Finished at: 2016-03-11T11:09:11+01:00
[INFO] Final Memory: 37M/427M
[INFO] ------------------------------------------------------------------------
public class ApplicationModule extends AbstractPresenterModule {
@Override
protected void configure() {
bindSingletonPresenterWidget(AppPresenter.class, AppPresenter.MyView.class, AppView.class);
}
}