////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
////


== set



=== Purpose


Sets the value of a variable accessible with the GSP page.


=== Examples


[,xml]
----
<g:set var="tomorrow" value="${new Date() + 1}" />

<g:set var="counter" value="${1}" />
<g:each in="${list}">
    ${counter}.&nbsp; ${it} -> ${counter % 2 == 0 ? 'even' : 'odd'}
    <g:set var="counter" value="${counter + 1}" /><br/>
</g:each>
----

Using scopes:

[,xml]
----
<g:set var="foo" value="${new Date()}" scope="page" />
<g:set var="bar" value="${new Date() - 7}" scope="session" />
----

Using the body contents:

[source,xml]
----
<g:set var="foo">Hello!</g:set>
----

Get a bean from the applicationContext by name:

[source,xml]
----
<g:set var="bookService" bean="bookService"/>
----

Get a bean from the applicationContext by type:

[,xml]
----
<%@ 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:

[,xml]
----
<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 of `request`, `page`, `flash` or `session`. Defaults to `page`.
* `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 alongside `value`, since a body or a `bean` produces its value as the tag runs.

