如何在GWT RPC调用中的会话过期后重定向到登录页面 [英] How to redirect to login page after session expire in GWT RPC call

查看:163
本文介绍了如何在GWT RPC调用中的会话过期后重定向到登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用GWT和RPC。在会话过期之后,当我进行RPC调用时,由于我的登录过滤器,请求重定向到login.jsp,但是我的问题是客户端不会向我显示login.jsp,而是引发RPC的onFailure。



这意味着我应该处理所有rpc的onFailure事件,以重定向到登录页面?!!!!



谢谢

解决方案

我同意 pathed ,你应该在你的 AsyncCallback 中重定向。但是,您不必显式使用自定义 MyAsyncCallback 回调,而不使用标准GWT AsyncCallback 。当你已经有很多使用标准回调的代码时,这很重要。



当你调用 GWT.create(MyService.class)时, GWT为您的 MyServiceAsync 服务接口生成代理。此代理负责与服务器进行通信,并在从服务器获取数据时负责调用回调。代理使用 GWT代码生成器机制和默认GWT生成使用 ServiceInterfaceProxyGenerator 类生成这些代理。您可以扩展这个默认生成器( ServiceInterfaceProxyGenerator class)自动在所有回调调用中使用自定义的MyAsyncCallbacks 。我们最近在一个项目中做到了这一点。下面是我们使用的源代码。



代码为 MyAsyncCallback ,它与< a href =https://stackoverflow.com/questions/3657572/how-to-redirect-to-login-page-after-session-expire-in-gwt-rpc-call/3658433#3658433> pathed a>:

  package my.package.client; 

import com.google.gwt.user.client.rpc.AsyncCallback;

public class MyAsyncCallback< T>实现AsyncCallback< T> {

private final AsyncCallback< T> AsyncCallback的;

public MyAsyncCallback(AsyncCallback< T> asyncCallback){
this.asyncCallback = asyncCallback;

$ b @Override
public void onFailure(可捕获的Throw){
if(捕获的instanceof SessionTimeoutException){
//重定向
return ;
}

asyncCallback.onFailure(caught);
}

@Override
public void onSuccess(T result){
asyncCallback.onSuccess(result);





GWT代码生成器的代码( MyRpcRemoteProxyGenerator ):

  package my.package.server; 

import com.google.gwt.core.ext.typeinfo.JClassType;
import com.google.gwt.user.rebind.rpc.ProxyCreator;
import com.google.gwt.user.rebind.rpc.ServiceInterfaceProxyGenerator;
$ b $ public class MyRpcRemoteProxyGenerator extends ServiceInterfaceProxyGenerator {
$ b @Override
protected ProxyCreator createProxyCreator(JClassType remoteService){
return new MyProxyCreator(remoteService);






生成器辅助类( MyProxyCreator ):

  package my.package.server; 

import java.util.Map;

import com.google.gwt.core.ext.typeinfo.JClassType;
import com.google.gwt.core.ext.typeinfo.JMethod;
import com.google.gwt.user.rebind.SourceWriter;
import com.google.gwt.user.rebind.rpc.ProxyCreator;
import com.google.gwt.user.rebind.rpc.SerializableTypeOracle;

$ b $ public class MyProxyCreator扩展ProxyCreator {

private final String methodStrTemplate =@ Override \\\

+protected< T> com .google.gwt.http.client.Request doInvoke(ResponseReader responseReader,
+String methodName,int invocationCount,String requestData,
+com.google.gwt.user.client.rpc。 AsyncCallback< T>回调){\ n
+$ {method-body}+} \ n;

public MyProxyCreator(JClassType serviceIntf){
super(serviceIntf);

$ b @Override
protected void generateProxyMethods(SourceWriter w,
SerializableTypeOracle serializableTypeOracle,
Map< JMethod,JMethod> syncMethToAsyncMethMap){
//生成标准的代理方法
super.generateProxyMethods(w,serializableTypeOracle,
syncMethToAsyncMethMap);

//产生额外的方法
overrideDoInvokeMethod(w);
}

private void overrideDovokeMethod(SourceWriter w){
StringBuilder methodBody = new StringBuilder();
methodBody
.append(final com.google.gwt.user.client.rpc.AsyncCallback newAsyncCallback = new my.package.client.MyAsyncCallback(callback); \\\
);
methodBody
.append(return super.doInvoke(responseReader,methodName,invocationCount,requestData,newAsyncCallback); \\\
);

String methodStr = methodStrTemplate.replace($ {method-body},
methodBody);
w.print(methodStr);
}

}

最后,您需要注册新的代码生成器用于生成异步服务的代理。这是通过将这添加到您的GWT配置文件(gwt.xml文件)中完成的:

 < generate-with 
class =my.package.server.MyRpcRemoteProxyGenerator>
< when-type-assignable class =com.google.gwt.user.client.rpc.RemoteService/>
< / generate-with>

一开始看起来似乎是一个非常复杂的解决方案:)但它有它的优势: / p>


  • 您仍然可以使用标准GWT AsyncCallback s

  • 当您的应用程序在全局会话超时时,您可以强制重定向

  • 您可以轻松地打开和关闭(通过添加或删除 generate-with 在你的GWT配置文件中)


I am using GWT and RPC in my app. after session expires when I do a RPC call, because of my login-filter the request redirect to login.jsp, but my problem is client doen't show me login.jsp instead the RPC's onFailure raised.

It means I should handle all my rpc's onFailure events for redirecting to login page ?!!!!

Thanks

解决方案

I agree with pathed that you should do redirecting in your AsyncCallbacks. However, you don't need to explicitly use your custom MyAsyncCallback callbacks instead of standard GWT AsyncCallback. This is important for example when you already have a lot of code that uses standard callbacks.

When you invoke GWT.create(MyService.class) GWT generates proxy for your MyServiceAsync service interface. This proxy is responsible for communicating with the server and invoking your callbacks when it gets data from the server. Proxies are generated using GWT code generators mechanism and by default GWT uses ServiceInterfaceProxyGenerator class to generate these proxies.

You can extend this default generator (ServiceInterfaceProxyGenerator class) to automatically use your custom MyAsyncCallbacks in all callbacks invocations. We recently did exactly that in a project. Below there is source code which we used.

Code for MyAsyncCallback, it is identical to the one presented by pathed:

package my.package.client;

import com.google.gwt.user.client.rpc.AsyncCallback;

public class MyAsyncCallback<T> implements AsyncCallback<T> {

    private final AsyncCallback<T> asyncCallback;

    public MyAsyncCallback(AsyncCallback<T> asyncCallback) {
        this.asyncCallback = asyncCallback;
    }

    @Override
    public void onFailure(Throwable caught) {
        if (caught instanceof SessionTimeoutException) {
            // redirect
            return;
        }

        asyncCallback.onFailure(caught);
    }

    @Override
    public void onSuccess(T result) {
        asyncCallback.onSuccess(result);
    }

}

Code for GWT code generator (MyRpcRemoteProxyGenerator):

package my.package.server;

import com.google.gwt.core.ext.typeinfo.JClassType;
import com.google.gwt.user.rebind.rpc.ProxyCreator;
import com.google.gwt.user.rebind.rpc.ServiceInterfaceProxyGenerator;

public class MyRpcRemoteProxyGenerator extends ServiceInterfaceProxyGenerator {

    @Override
    protected ProxyCreator createProxyCreator(JClassType remoteService) {
        return new MyProxyCreator(remoteService);
    }
}

And generator helper class (MyProxyCreator):

package my.package.server;

import java.util.Map;

import com.google.gwt.core.ext.typeinfo.JClassType;
import com.google.gwt.core.ext.typeinfo.JMethod;
import com.google.gwt.user.rebind.SourceWriter;
import com.google.gwt.user.rebind.rpc.ProxyCreator;
import com.google.gwt.user.rebind.rpc.SerializableTypeOracle;


public class MyProxyCreator extends ProxyCreator {

    private final String methodStrTemplate = "@Override\n"
            + "protected <T> com.google.gwt.http.client.Request doInvoke(ResponseReader responseReader, "
            + "String methodName, int invocationCount, String requestData, "
            + "com.google.gwt.user.client.rpc.AsyncCallback<T> callback) {\n"
            + "${method-body}" + "}\n";

    public MyProxyCreator(JClassType serviceIntf) {
        super(serviceIntf);
    }

    @Override
    protected void generateProxyMethods(SourceWriter w,
            SerializableTypeOracle serializableTypeOracle,
            Map<JMethod, JMethod> syncMethToAsyncMethMap) {
        // generate standard proxy methods
        super.generateProxyMethods(w, serializableTypeOracle,
                syncMethToAsyncMethMap);

        // generate additional method
        overrideDoInvokeMethod(w);
    }

    private void overrideDoInvokeMethod(SourceWriter w) {
        StringBuilder methodBody = new StringBuilder();
        methodBody
                .append("final com.google.gwt.user.client.rpc.AsyncCallback newAsyncCallback = new my.package.client.MyAsyncCallback(callback);\n");
        methodBody
                .append("return super.doInvoke(responseReader, methodName, invocationCount, requestData, newAsyncCallback);\n");

        String methodStr = methodStrTemplate.replace("${method-body}",
                methodBody);
        w.print(methodStr);
    }

}

Finally you need to register the new code generator to be used for generating proxies for async services. This is done by adding this to your GWT configuration file (gwt.xml file):

<generate-with
    class="my.package.server.MyRpcRemoteProxyGenerator">
    <when-type-assignable class="com.google.gwt.user.client.rpc.RemoteService" />
</generate-with>

At the beginning it may seem to be a very complicated solution :) but it has its strengths:

  • You can still use standard GWT AsyncCallbacks
  • You can enforce redirecting when session times out globally for your application
  • You can easily tun it on and off (by adding or removing generate-with in your GWT config files)

这篇关于如何在GWT RPC调用中的会话过期后重定向到登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆