set
用途
在GSP页面中,用于声明变量,为变量设值.
示例
<g:set var="tomorrow" value="${new Date(today.getTime() + 24L * 60 * 60 * 1000)}" />
<g:sef var="counter" value="${1}" />
<g:each in="${list}">
${counter}. ${it} -> ${counter % 2 == 0 ? 'even' : 'odd'}
<g:set var="counter" value="${counter + 1}" /><br>
</g:each>
设置作用域:
<g:set var="foo" value="${new Date()}" scope="page" />
<g:set var="bar" value="${new Date()-7}" scope="session" />
也可以这样使用:
<g:set var="foo">Hello!</g:set>
描述
Attributes
var
- 变量的名称
value
- 设置初始化的值
scope
- 设置变量的作用域 (可选参数: request
, page
, flash
或 session
. 默认下为 page 属性
).
Source
Show Source
def set = { attrs, body ->
def scope = attrs.scope ? SCOPES[attrs.scope] : 'pageScope'
def var = attrs.var
def value = attrs.value
def containsValue = attrs.containsKey('value')
if(!scope) throw new IllegalArgumentException("Invalid [scope] attribute for tag <g:set>!")
if(!var) throw new IllegalArgumentException("[var] attribute must be specified to for <g:set>!") if(!containsValue && body) value = body() this."$scope"."$var" = value
}