2012-03-19 56 views
0

我們可以通過servletContext(或grailsApplication)在Grails BootStrap中訪問HttpSession嗎?當用戶登錄到應用程序時,我在會話中存儲了一些信息,並在動態方法beforeInsert中使用它們。但我不知道如何在會話中檢索這些信息。
這裏是我的引導代碼:從Grails中的servletContext訪問HttpSession BootStrap

class BootStrap { 
    def grailsApplication 
    def init = { servletContext -> 
     grailsApplication.getArtefacts("Domain").each { org.codehaus.groovy.grails.commons.GrailsDomainClass gc -> 
      gc.metaClass.beforeInsert = { 
       // Do something here with information stored in session 
      } 

      gc.metaClass.beforeUpdate = { 
           // Do something here with information stored in session 
        } 
     } 
    } 
    def destroy = { 
    } 
} 

謝謝你這麼多

回答

0

沒有,因爲一個servlet上下文有多個會話。會話是每個用戶,而上下文是全局的。所以範圍不匹配。

你可以做的是有一個HttpSessionListener監聽會話創建和會話刪除。我猜你可以以某種方式訪問​​在服務器關閉後序列化到磁盤的會話,但這會要求你干涉本地tomcat實現(可能有些Valve)