---
title: "grails 中的session、params哪里来的"
author: "bluedest"
date: "2012-05-21T11:59:18Z"
tags: []
categories: ["编程语言"]
slug: "grails-session-params"
draft: false
lastmod: "2012-05-21T11:59:18Z"
id: 1536175
url: "https://bluedest.iteye.com/blog/1536175"
---

查看 org.codehaus.groovy.grails.web.plugins.support.WebMetaUtils，其中有一个方法，看完就明白了

 

```
/**
     * This creates the difference dynamic methods and properties on the controllers. Most methods
     * are implemented by looking up the current request from the RequestContextHolder (RCH)
     */
    static registerCommonWebProperties(MetaClass mc, GrailsApplication application) {
        def paramsObject =         { -> RCH.currentRequestAttributes().params }
        def flashObject =          { -> RCH.currentRequestAttributes().flashScope }
        def sessionObject =        { -> RCH.currentRequestAttributes().session }
        def requestObject =        { -> RCH.currentRequestAttributes().currentRequest }
        def responseObject =       { -> RCH.currentRequestAttributes().currentResponse }
        def servletContextObject = { -> RCH.currentRequestAttributes().servletContext }
        def grailsAttrsObject =    { -> RCH.currentRequestAttributes().attributes }

        // the params object
        mc.getParams = paramsObject
        // the flash object
        mc.getFlash = flashObject
        // the session object
        mc.getSession = sessionObject
        // the request object
        mc.getRequest = requestObject
        // the servlet context
        mc.getServletContext = servletContextObject
        // the response object
        mc.getResponse = responseObject
        // The GrailsApplicationAttributes object
        mc.getGrailsAttributes = grailsAttrsObject
        // The GrailsApplication object
        mc.getGrailsApplication = { -> RCH.currentRequestAttributes().attributes.grailsApplication }

        mc.getActionName =     { -> RCH.currentRequestAttributes().actionName }
        mc.getControllerName = { -> RCH.currentRequestAttributes().controllerName }
        mc.getWebRequest =     { -> RCH.currentRequestAttributes() }
    }
```