1

我的 GWT 应用程序中有这么脏的代码,我的服务层的一些类依赖于HttpSession对象。例如,在我的一个 DAO(它是一个 GWT-RPC 端点)中,我有这样的东西:

public class MyExampleDAO extends RemoteServiceServlet {
   public findItems() {
       // here I need to get the object session to retrieve the currently logged in user in order to query for all its items...
   }
}

问题是,我目前正在迁移代码以使用 RequestFactory。我的 DAO 将不再是 GWT-RPC 端点。所以不需要扩展 RemoteServiceServlet ......

你知道我如何获取/注入(可能使用Guice)我对 HttpSession 对象的依赖,知道我的类不再扩展 RemoteServiceServlet 吗?

4

1 回答 1

1

getThreadLocalRequest().getSession()应该这样做。RequestFactoryServlet有一个类似的(但静态的)getThreadLocalRequest()方法,您可以从您的服务访问。否则,您可以让 Guice 注入一个Provider<HttpSession>,看看这些项目https://github.com/mgenov/injecting-request-factoryhttps://github.com/etiennep/injected-requestfactory使用一些示例代码使用 RequestFactory 的 Guice。

于 2011-06-03T11:50:00.710 回答