set
Sets a variable of the defined name and value into the variable context, overwriting any existing variable with the same name.
If placed as a child of a looping command or any other command that creates a local variable context, set
has the special capability of being able to set a new value for an already existing variable in a parent variable context. The behavior of set
in a local variable context in different conditions is as follows:
The local context does not have a variable with the name of
set
'svar
, but a parent context does. In this caseset
sets a new value for the parent context variable.The local context has a variable with the name of
set
'svar
. In this caseset
's sets a new value for this local context variable, regardless of the parent contexts' variables.The local context does not have a variable with the name of
set
'svar
, and neither does any of the parent contexts. In this caseset
creates a new local context variable.
Attributes
var | ||
---|---|---|
Required | Value type | EL-evaluated |
Yes | String | No |
Defines the name of the variable that will hold the value. |
value | ||
---|---|---|
Required | Value type | EL-evaluated |
No | Any | Yes |
Whatever value this attribute's expression resolves into is set into the variable named with If not defined or if the value expression resolves into |
required | ||
---|---|---|
Required | Value type | EL-evaluated |
No | Boolean | No |
If this attribute resolves into If not defined, value of |
format | ||
---|---|---|
Required | Value type | EL-evaluated |
No | String | Yes |
If the resolved value of this attribute is the name of a Format, that Format is used format the resolved value of If both If undefined or if the named Format cannot be found, no formatting is done to the value. |
parser | ||
---|---|---|
Required | Value type | EL-evaluated |
No | String | Yes |
If the resolved value of this attribute is the name of a Parser, the resolved value of If both If undefined or if the named Parser cannot be found, the value remains retains its original type. |
Examples
The set
command has a variety of uses despite its simple nature. One common use is to copy parameter values of the Flow into more conveniently named variables. This also makes these values available in contexts where the parameter map is not accessible.
<set var="id" value="${param.id}">
Besides duplicating values of existing variables, set
can be used to create entirely new variables. Creating a String only requires the value
attribute to not be an EL-expression.
<set var="defaultWeather" value="Somewhat gusty">
Boolean values can be set with EL-expressions ${true}
and ${false}
.
<set var="doesItWork" value="${true}">
The parser
attribute can be used to convert existing variables into other types of variables. Converting a String representing a number into an actual Number, for example, allows the value to used in mathematical EL-expressions. Just be sure to use a number parser suited to parsing number strings of the given format.
<set var="numberStr" value="122,25"/><set var="actualNumber" value="${numberStr}" parser="numberParser"/>
This command's unique property to allow overwriting values of variables in parent variable contexts is also essential at times. Any other variable-producing command places its variable into its current context, which, for example, inside a forEach loop is the local context of its loop round that will terminate when the round is finished.
In this example, if the set
preceding forEach
didn't exist, the variable created by the set
inside the forEach
would just disappear when forEach
is done. But with the first set
creating the variable in the base variable context, the inner set
sets its value there as well, allowing the value to exist past the loop.
<set var="soughtContactId" value="-"><forEach value="${contacts}" var="${contact}"><if test="${contact.Name == soughtContactName}"><set var="soughtContactId" value="${contact.Id}">