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

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

问题描述

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

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.

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

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

谢谢

推荐答案

我同意 路径 您应该在 AsyncCallback 中进行重定向.但是,您不需要显式使用自定义 MyAsyncCallback 回调代替标准 GWT AsyncCallback.例如,当您已经有大量使用标准回调的代码时,这很重要.

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.

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

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.

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

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.

MyAsyncCallback 的代码,与 路径:

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);
    }

}

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

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);
    }
}

和生成器辅助类(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
"
            + "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) {
"
            + "${method-body}" + "}
";

    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);
");
        methodBody
                .append("return super.doInvoke(responseReader, methodName, invocationCount, requestData, newAsyncCallback);
");

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

}

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

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:

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

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

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