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:
set
's var
, but a
parent context does. In this case set
sets a new value for the parent context variable.set
's var
. In this case
set
's sets a new value for this local context variable, regardless of the parent contexts'
variables.
set
's var
, and
neither does any of the parent contexts. In this case set
creates a new local context variable.
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 var .If not defined or if the value expression resolves into null , no variable is set. Additionally, if the required
attribute resolves into true , an error is produced. |
required | ||
---|---|---|
Required |
Value type |
EL-evaluated |
No | Boolean | No |
If this attribute resolves into true , the
value attribute resolving into null produces an error.If not defined, value of false is used.
|
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 value into a String.If both parser and format are defined, and the parsing process is successful, the
value to be formatted is the result of the parsing.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 value is first converted into a String (if it isn't one already)
and then parsed into another type of value by the Parser.If both parser and
format attributes are defined, the parsing process is done before the
formatting.If undefined or if the named Parser cannot be found, the value remains retains its original type. |
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}">