会话在GWT RPC调用中到期后如何重定向到登录页面

Nav*_*Nav 13 gwt rpc

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

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

谢谢

Pio*_*otr 21

我同意径处理,你应该在你做重定向AsyncCallback秒.但是,您不需要显式使用自定义MyAsyncCallback回调而不是标准GWT AsyncCallback.这很重要,例如,当您已经拥有大量使用标准回调的代码时.

当您调用GWT.create(MyService.class)GWT 时,会为您的MyServiceAsync服务接口生成代理.此代理负责与服务器通信并在从服务器获取数据时调用回调.代理是使用GWT代码生成器机制生成的,默认情况下GWT使用ServiceInterfaceProxyGeneratorclass来生成这些代理.

您可以扩展此默认生成器(ServiceInterfaceProxyGenerator类)以在所有回调调用中自动使用自定义MyAsyncCallbacks.我们最近在一个项目中做到了这一点.下面是我们使用的源代码.

代码MyAsyncCallback,它与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);
    }

}
Run Code Online (Sandbox Code Playgroud)

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;

public class MyRpcRemoteProxyGenerator extends ServiceInterfaceProxyGenerator {

    @Override
    protected ProxyCreator createProxyCreator(JClassType remoteService) {
        return new MyProxyCreator(remoteService);
    }
}
Run Code Online (Sandbox Code Playgroud)

和生成器助手类(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);
    }

}
Run Code Online (Sandbox Code Playgroud)

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

<generate-with
    class="my.package.server.MyRpcRemoteProxyGenerator">
    <when-type-assignable class="com.google.gwt.user.client.rpc.RemoteService" />
</generate-with>
Run Code Online (Sandbox Code Playgroud)

一开始它似乎是一个非常复杂的解决方案:)但它有它的优点:

  • 您仍然可以使用标准的GWT AsyncCallback小号
  • 您可以在会话超出全局范围时为您的应用程序强制重定向
  • 您可以轻松地打开和关闭它(通过generate-with在GWT配置文件中添加或删除)


pat*_*hed 5

是的,您应该在onFailure中处理会话超时(在我看来).但有一些简单的方法可以做到这一点.

  1. 实现自己的异步回调.

    public abstract class MyAsyncCallback<T> implements AsyncCallback<T> {
    
    @Override
    public void onFailure(Throwable arg0) {
        if arg0 is SessionTimeout
            redirect to loginpage
        else
            failure(Throwable ar0)
    }
    
    @Override
    public void onSuccess(T arg0) {
        success(arg0);
    }
    
    public abstract void success(T arg0);
    
    public abstract void failure(Throwable arg0);
    
    Run Code Online (Sandbox Code Playgroud)

    }

  2. 使用像gwt-dispatcher这样的库,其中所有rpc调用都通过相同的serviceasync并给你一个地方来处理onFailures.