如何從命令對象中獲取會話?在Grails中獲取命令對象中的會話
我曾嘗試:
import org.springframework.security.context.SecurityContextHolder as SCH
class MyCommand {
def session = RCH.currentRequestAttributes().getSession()
}
這將引發
java.lang.IllegalStateException:沒有線程綁定請求中找到:你是指請求屬性實際web請求之外,或處理請求在原始接收線程之外?如果實際上在Web請求中運行並仍然收到此消息,則代碼可能在DispatcherServlet/DispatcherPortlet之外運行:在這種情況下,請使用RequestContextListener或RequestContextFilter來公開當前請求。
謝謝,我是否需要在我的驗證器中調用「someMethod()」? – RyanLynch 2010-05-05 22:36:13
不,您可以直接在驗證器中訪問SRH.request.session,因爲它將在http請求的上下文中調用。你無法將其定義爲一個字段。 – 2010-05-05 22:46:31