6.1.2 Controllers and Scopes

h4. Available Scopes
h4.可用范围(Scopes)
Scopes are essentially hash like objects that allow you to store variables. The following scopes are available to controllers:
范围(Scopes)本质上就像hash对象,允许你存储变量。下列为controllers(控制器)可用范围(Scopes):
* [servletContext|controllers] - Also known as application scope, this scope allows you to share state across the entire web application. The servletContext is an instance of [javax.servlet.ServletContext|api:javax.servlet.ServletContext]
* [servletContext|controllers] - 也被叫做应用(application)范围, 这个范围允许你横跨整个web应用程序共享状态. servletContext对象为一个[javax.servlet.ServletContext|api:javax.servlet.ServletContext]实体。
* [session|controllers] - The session allows associating state with a given user and typically uses cookies to associate a session with a client. The session object is an instance of [HttpSession|api:javax.servlet.http.HttpSession]
* [session|controllers] - session允许关联某个给定用户的状态,通常使用Cookie把一个session与一位客户关联起来  ,session对象为一个[HttpSession|api:javax.servlet.http.HttpSession]实体
* [request|controllers] - The request object allows the storage of objects for the current request only. The request object is an instance of [HttpServletRequest|api:javax.servlet.http.HttpServletRequest]
* [request|controllers] - request对象只允许存储当前的请求(request)对象,request 对象为一个[HttpServletRequest|api:javax.servlet.http.HttpServletRequest]实体
* [params|controllers] - Mutable map of incoming request (CGI) parameters
* [params|controllers] - 可变的进入请求参数map(map为java.util.Map类型参数)。
* [flash|controllers] - See below.
* [flash|controllers] - 见下文.
h4. Accessing Scopes
h4. 存取范围(Scopes)
Scopes can be accessed using the variable names above in combination with Groovy's array index operator even on classes provided by the Servlet API such as the [HttpServletRequest|api:javax.servlet.http.HttpServletRequest]:
Scopes的存取可以通过使用上面变量名与Groovy 的array索引操作符的结合来访问,甚至可以使用Servlet API提供的类,例如HttpServletRequest : [HttpServletRequest|api:javax.servlet.http.HttpServletRequest]:

{code:java}
class BookController {
    def find = {
        def findBy = params["findBy"]
        def appContext = request["foo"]
        def loggedUser = session["logged_user"]

    }
}
{code}

You can even access values within scopes using the de-reference operator making the syntax even clearer:
你甚至可以使用.操作符来存取范围(Scopes)内部值,这样使语法更加简洁清楚:
{code:java}
class BookController {
    def find = {
        def findBy = params.findBy
        def appContext = request.foo
        def loggedUser = session.logged_user

    }
}
{code}

This is one of the ways that Grails unifies access to the different scopes.
这是统一存取不同范围的方式之一.
h4. Using Flash Scope
h4. 使用 Flash Scope
Grails supports the concept of [flash|controllers] scope is a temporary store for attributes which need to be available for this request and the next request only. Afterwards the attributes are cleared. This is useful for setting a message directly before redirection, for example:
Grails 支持[flash|controllers] scope的概念,它临时存贮只在这次请求和下次请求中使用的属性,随后属性值将被清除。这在重定向之前直接设置消息是非常有用的。
{code:java}
def delete = {
    def b = Book.get( params.id )
    if(!b) {
        flash.message = "User not found for id ${params.id}"
        redirect(action:list)
    }
    ... // remaining code
}
{code}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值