<g:set var="tomorrow" value="${new Date() + 1}" />
<g:set 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>
set
Purpose
Sets the value of a variable accessible with the GSP page.
Examples
Using scopes:
<g:set var="foo" value="${new Date()}" scope="page" />
<g:set var="bar" value="${new Date() - 7}" scope="session" />
Using the body contents:
<g:set var="foo">Hello!</g:set>
Get a bean from the applicationContext by name:
<g:set var="bookService" bean="bookService"/>
Get a bean from the applicationContext by type:
<%@ page import="org.springframework.context.MessageSource" %>
<g:set var="messageSource" bean="${MessageSource}"/>
Give the variable a type, so that a statically compiled page knows what it holds:
<g:set type="int" var="counter" value="${1}"/>
${counter + 1}
Description
Attributes
-
var- The name of the variable -
value- The initial value -
bean- The name or the type of a bean in the applicationContext; the type can be an interface or superclass -
scope- Scope to set variable in, one ofrequest,page,flashorsession. Defaults topage. -
type- The type to declare the variable as, so that a page compiled statically can apply an operator to it. The value is converted to the type. Only valid alongsidevalue, since a body or abeanproduces its value as the tag runs.