GWT - RemoteService接口和Spring - 如何获取HttpSession?

时间:2013-04-16 14:07:32

标签: spring gwt httpsession

我正在为我的项目使用带有RPC,Spring和Postgresql的GWT(2.5)。我的问题是关于HttpSession处理。 所有对服务器的查询都由Spring(DispatchServlet)调度到我的GwtController(扩展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 !!!!!
    …………………………

}

}

1 个答案:

答案 0 :(得分:3)

您可以在ThreadLocal中维护Servlet并将当前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();
    }
 }