2011-03-13 40 views
2

我的用戶域有一個名爲'lastLogin'的域,我想在登錄後自動更新(self-exaplantoy)。登錄後更新用戶的lastLogin屬性

groovy.lang.MissingMethodException:法無簽名: 重啓動Tomcat(DEV ENV)我得到下面的異常後靜態photoo.user.User.findById()是適用於參數類型:(java.lang中。長)的值:[2]可能的解決方案:的findAll(groovy.lang.Closure),找到(groovy.lang.Closure)

我使用在我Config.groovy中以下代碼:

// callback event, after a successful login 
grails.plugins.springsecurity.onInteractiveAuthenticationSuccessEvent = { e, appCtx -> 
    def user = User.findById(appCtx.springSecurityService.principal.id) 
    User.withTransaction { 
     if(!user.isAttached()) 
      user.attach() 
     user.lastLogin = new Date() 
     user.save(flush: true) 
    } 
} 

如何避免異常,爲什麼發生? thx

回答

1

您可能只需要使用User.get()。這是我的:

onInteractiveAuthenticationSuccessEvent = { e, appCtx -> 

    // handle AuthenticationSuccessEvent 
    def autservice = appCtx.authenticateService 
    def user = autservice.userDomain() 
    println "user: ${user.id}:${user}}" 
    def request = org.codehaus.groovy.grails.plugins.springsecurity.SecurityRequestHolder.getRequest() 
    def session = request.getSession(false) 
    if (user) { 

     def person = Person.get(user.id) 

     def lang = person.preferredLanguage 
     if (session) { 
      session.lang = lang 
     } 
    } 
} 
3

終於我發現了錯誤。將用戶加載到withTransaction塊是解決方案。

// dont load before 
// def user = User.findById(appCtx.springSecurityService.principal.id) 
User.withTransaction { 
     def user = User.findById(appCtx.springSecurityService.principal.id) 
     if(!user.isAttached()) 
      user.attach() 
     user.lastLogin = new Date() 
     user.save(flush: true, failOnError: true) 
    }