实现GWTP placeManager的其他方式不是构造函数的@Inject注释 [英] Other way to achive GWTP placeManager than Constructor's @Inject annotation
问题描述
我使用GWTP和restyGWT。我想在restyGWT DispatcherCallback中使用placeManager,当我的rest服务器将回答 401未授权
我想将应用程序重定向到登录页面时,该用户可以应用凭证并重试他请求。
为此,我必须以某种方式获取PlaceManager的实例(来自gwtp框架)。我不能使用 @Inject
注释,因为我手动调用构造函数如下:
public class ForbiddenDispatcherFilter implements DispatcherFilter {
@Override
public boolean filter(Method method,RequestBuilder builder){
builder.setCallback(new ForbiddenDispatcherCallback(method));
返回true;
}
}
public class ForbiddenDispatcherCallback实现RequestCallback {
protected RequestCallback requestCallback;
public ForbiddenDispatcherCallback(方法方法){
this.requestCallback = method.builder.getCallback();
$ b @Override
public void onResponseReceived(Request request,Response response){
if(response.getStatusCode()== Response.SC_FORBIDDEN || response。 getStatusCode()== Response.SC_UNAUTHORIZED){
//将硬重定向到登录页面
// TODO更改重定向到GWTP本地
Window.Location.assign(#login) ;
// PlaceRequest placeRequest = new
// PlaceRequest.Builder(placeManager.getCurrentPlaceRequest())。nameToken(Routing.Url.login).build();
// placeManager.revealPlace(placeRequest);
} else {
requestCallback.onResponseReceived(request,response);
$ b公共类RestyDispatcher扩展了DefaultFilterawareDispatcher {
public RestyDispatcher(){
addFilter (new ForbiddenDispatcherFilter());
addFilter(new BasicAuthHeaderDispatcherFilter());
$ b @Override
public请求发送(Method method,RequestBuilder builder)throws RequestException {
return super.send(method,builder);
}
}
请帮助。 和 您如何以及在哪里创建您的 您可以使用guice的 AssistedInjection 将 您需要定义一个工厂界面: 在 然后你可以用这种方式实例化你的类: 当然这需要你注入 您可以尝试传递 I use GWTP and restyGWT. I would like to use placeManager in restyGWT DispatcherCallback, when my rest server will answer with To do this I have to somehow get instance of PlaceManager (from gwtp framework). I cannot use Please help. Edit and:
How and where do you create your You could use guice's AssistedInjection to inject the You need to define an factory interface: In the Then you can instantiate your class this way: Of course this requires that you also inject the You could try to pass the The
这篇关于实现GWTP placeManager的其他方式不是构造函数的@Inject注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
编辑
p $ p $ 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(新的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);
// Google Analytics
// bindConstant()。annotatedWith(GaAccount.class).to(UA-8319339-6);
//加载并注入CSS资源
bind(ResourceLoader.class).asEagerSingleton();
$ b
public class RestyGwtConfig {
static {
// GWT.log ( - > RestyGwtConfig - > setDispatcher);
Defaults.setDispatcher(new RestyDispatcher());
// GWT.log( - > RestyGwtConfig - > setServiceRoot);
Defaults.setServiceRoot(new Resource(GWT.getModuleBaseURL())。resolve(ServiceRouting.SERVICE_ROOT).getUri());
UserCredentials.INSTANCE.setUserName(ronan);
UserCredentials.INSTANCE.setPassword(password);
}
}
ForbiddenDispatcherFilter
?
PlaceManager
注入到 ForbiddenDispatcherCallback
中。
public class ForbiddenDispatcherCallback implements RequestCallback {
protected RequestCallback requestCallback;
受保护的PlaceManager placeManager;
@Inject
public ForbiddenDispatcherCallback(PlaceManager placeManager,@Assisted Method方法){
this.placeManager = placeManager;
this.requestCallback = method.builder.getCallback();
public interface AssistedInjectionFactory {
ForbiddenDispatcherCallback createForbiddenCallback(Method method);
$ / code> 配置
你需要调用 $ b
install(new GinFactoryModuleBuilder ().build(AssistedInjectionFactory.class));
public class ForbiddenDispatcherFilter实现DispatcherFilter {
AssistedInjectionFactory factory;
@Inject
public ForbiddenDispatcherFilter(AssistedInjectionFactory factory)
{
this.factory = factory;
$ b @Override
public boolean filter(Method method,RequestBuilder builder){
builder.setCallback(factory.AssistedInjectionFactory(method))
return真正;
ForbiddenDispatcherFilter
。
编辑:
RestyDispatcher
给你的 RestyGWTConfig
的构造函数:
$ b
public class RestyGwtConfig {
@Inject
public RestyGwtConfig(RestyDispatcher dispatcher){
Defaults.setDispatcher(dispatcher);
}
static {
// GWT.log( - > RestyGwtConfig - > setServiceRoot);
Defaults.setServiceRoot(new Resource(GWT.getModuleBaseURL())。resolve(ServiceRouting.SERVICE_ROOT).getUri());
UserCredentials.INSTANCE.setUserName(ronan);
UserCredentials.INSTANCE.setPassword(password);
RestyDispatcher
看起来像这样:
public class RestyDispatcher extends DefaultFilterawareDispatcher {
@Inject
public RestyDispatcher(ForbiddenDispatcherFilter过滤器){
addFilter(filter);
addFilter(new BasicAuthHeaderDispatcherFilter());
$ b @Override
public请求发送(Method method,RequestBuilder builder)throws RequestException {
return super.send(method,builder);
}
}
401 unauthorised
I would like to redirect application to login page, that User could apply credentials and retried his request.@Inject
annotation, cause I have manuall call to constructor as follow:public class ForbiddenDispatcherFilter implements DispatcherFilter {
@Override
public boolean filter(Method method, RequestBuilder builder) {
builder.setCallback(new ForbiddenDispatcherCallback(method));
return true;
}
}
public class ForbiddenDispatcherCallback implements RequestCallback {
protected RequestCallback requestCallback;
public ForbiddenDispatcherCallback(Method method) {
this.requestCallback = method.builder.getCallback();
}
@Override
public void onResponseReceived(Request request, Response response) {
if (response.getStatusCode() == Response.SC_FORBIDDEN || response.getStatusCode() == Response.SC_UNAUTHORIZED) {
// make a hard redirect to login page
// TODO change redirect to GWTP native
Window.Location.assign("#login");
// PlaceRequest placeRequest = new
// PlaceRequest.Builder(placeManager.getCurrentPlaceRequest()).nameToken(Routing.Url.login).build();
// placeManager.revealPlace(placeRequest);
} else {
requestCallback.onResponseReceived(request, response);
}
}
public class RestyDispatcher extends DefaultFilterawareDispatcher {
public RestyDispatcher() {
addFilter(new ForbiddenDispatcherFilter());
addFilter(new BasicAuthHeaderDispatcherFilter());
}
@Override
public Request send(Method method, RequestBuilder builder) throws RequestException {
return super.send(method, builder);
}
}
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);
// Google Analytics
// bindConstant().annotatedWith(GaAccount.class).to("UA-8319339-6");
// Load and inject CSS resources
bind(ResourceLoader.class).asEagerSingleton();
}
}
public class RestyGwtConfig {
static {
// GWT.log("--> RestyGwtConfig -> setDispatcher");
Defaults.setDispatcher(new RestyDispatcher());
// GWT.log("--> RestyGwtConfig -> setServiceRoot");
Defaults.setServiceRoot(new Resource(GWT.getModuleBaseURL()).resolve(ServiceRouting.SERVICE_ROOT).getUri());
UserCredentials.INSTANCE.setUserName("ronan");
UserCredentials.INSTANCE.setPassword("password");
}
}
ForbiddenDispatcherFilter
? PlaceManager
into your ForbiddenDispatcherCallback
. public class ForbiddenDispatcherCallback implements RequestCallback {
protected RequestCallback requestCallback;
protected PlaceManager placeManager;
@Inject
public ForbiddenDispatcherCallback(PlaceManager placeManager, @Assisted Method method) {
this.placeManager = placeManager;
this.requestCallback = method.builder.getCallback();
}
}
public interface AssistedInjectionFactory {
ForbiddenDispatcherCallback createForbiddenCallback(Method method);
}
configure
method of your ClientModule
you need to call:install(new GinFactoryModuleBuilder().build(AssistedInjectionFactory.class));
public class ForbiddenDispatcherFilter implements DispatcherFilter {
AssistedInjectionFactory factory;
@Inject
public ForbiddenDispatcherFilter(AssistedInjectionFactory factory)
{
this.factory = factory;
}
@Override
public boolean filter(Method method, RequestBuilder builder) {
builder.setCallback(factory.AssistedInjectionFactory(method))
return true;
}
}
ForbiddenDispatcherFilter
. Edit:
RestyDispatcher
to the constructor of your RestyGWTConfig
:public class RestyGwtConfig {
@Inject
public RestyGwtConfig(RestyDispatcher dispatcher) {
Defaults.setDispatcher(dispatcher);
}
static {
// GWT.log("--> RestyGwtConfig -> setServiceRoot");
Defaults.setServiceRoot(new Resource(GWT.getModuleBaseURL()).resolve(ServiceRouting.SERVICE_ROOT).getUri());
UserCredentials.INSTANCE.setUserName("ronan");
UserCredentials.INSTANCE.setPassword("password");
}
}
RestyDispatcher
looks like this: public class RestyDispatcher extends DefaultFilterawareDispatcher {
@Inject
public RestyDispatcher(ForbiddenDispatcherFilter filter) {
addFilter(filter);
addFilter(new BasicAuthHeaderDispatcherFilter());
}
@Override
public Request send(Method method, RequestBuilder builder) throws RequestException {
return super.send(method, builder);
}
}