2

我将 GWT (2.5) 与 RPC、Spring 和 Postgresql 一起用于我的项目。我的问题是关于 HttpSession 处理。所有对服务器的查询都由 Spring (DispatchServlet) 分派到我的 GwtController (extends RemoteServiceServlet) 。特定的 RemoteService 被注入到 GwtController 中。在 GwtContorller 中获取 HttpSession 很容易。例如通过 getThreadLocalRequest().getSession() 或仅来自 request.getSession()。我的问题是如何在 RemoteService 中获取 HttpSession 对象?

public class GwtRpcController extends RemoteServiceServlet {
……………
private RemoteService remoteService;
private Class remoteServiceClass;
………………

public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
    …………
} 

public String processCall(String payload) throws SerializationException {
    …………
}

public void setRemoteService(RemoteService remoteService) {
    …………….
}

}

我的接口 - 实现 RemoteService 的 DataService

public class DataServiceImpl implements DataService {

public Data getData(){

    !!!!! Here I want to get HttpSession !!!!!
    …………………………

}

}
4

1 回答 1

3

您可以在其中维护 a并将ThreadLocalServlet的 current 存储在那里Request,然后Request使用静态方法公开您的。

public class GwtRpcController extends RemoteServiceServlet {

  static ThreadLocal<HttpServletRequest> perThreadRequest = 
         new ThreadLocal<HttpServletRequest>();

  @Override
  public String processCall(String payload) throws SerializationException {
    try {
      perThreadRequest.set(getThreadLocalRequest());
      return super.processCall(payload);
    } finally {
      perThreadRequest.set(null);
    }
  }

  public static HttpServletRequest getRequest() {
    return perThreadRequest.get();
  }
}


 public class DataServiceImpl implements DataService {
    public Data getData(){
       HttpServletRequest request = GwtRpcController.getRequest();
       HttpSession session = request.getSession();
    }
 }
于 2013-04-16T14:41:58.337 回答