Search code examples
grailsfilterbefore-filter

I want to access a variable declared in filter


def filters = {
     forUser(controller:'user', action:'*') {
         before = {
            user=springSecurityService.getCurrentUser()
            log.info("came to filter and the user is" + user)
         }
         after = {

         }
         afterView = {

         }
     }
}

I want to access this user variable in the user controller. Is it possible without making a session variable?


Solution

  • You can use request-scope variable:

    request.setAttribute('user', user)
    

    and use it in GSP as:

    ${user}
    

    or in controller:

    request['user']
    //or
    request.user