Defining conditional symbols
Within the <symbolDefinitions> tag, you can define a conditional
symbol by using the <conditionalSymbol> tag. A conditional symbol represents a value that is
acquired by evaluation a series of condition expressions. If any expression returns
true, request mapper uses the member to calculate the return value. When a member
meets the condition expression, request mapper evaluates a corresponding return expression, and
return the result. After the request mapper finds a result to return, it does not evaluate any
further expressions.
- <name>
- The name of the symbol. It is a string and must start with the
$character.
- <type>
- The type of the value that the symbol returns. Specify this value as a fully qualified Java™ class name, or a Java primitive. Specifying the type for the symbol is optional. If it is not defined, the request mapper attempts to establish the field type based on the expression. If the request mapper is unable to determine the symbol type before it evaluates the expression, performance is affected. Therefore, for best performance, it is better to specify the type.
- <args>
- The arguments for the symbol. This tag is optional; if it is specified, arguments must be supplied for evaluating the symbol. For more information, see Defining symbol arguments.
- <if condition="expression">
- The
conditionattribute defines a condition expression to evaluate. The expression must yield a Boolean value. If the value istrue, request mapper uses the contents of the <if> tag to try to determine the return value. The <if> tag must contain either, but not both, of the following contents:- A <return> tag. This tag contains an expression. If the condition expression is true, request mapper evaluates the expression and returns the result.
- Any number of <if> tags, nested within this <if> tag. If the condition expression is
true, request mapper processes the nested <if> tags in the same way as a top-level <if>
tag. That is, it evaluates the expression in the
conditionattribute, and if the expression is true, uses the contents of the tag to try and determine the return value.
Important: If a return value is determined, request mapper does not evaluate any further expressions. However, if a condition expression in an <if> tag is true, but it contains nested <if> tags and none of their condition expressions are true, no value is determined. In this case, request mapper continues to evaluate subsequent expressions.
- <defaultValue>
- Optional. If request mapper has evaluated all condition expressions, but none of the condition
expression has returned
true, request mapper evaluates the expression in the <defaultValue> tag. The conditional symbol returns the value that the expression produces. If this tag is not present, the default value isnull.
Example
<symbol>
<name>$GET</name>
<eval>"GET"</eval>
</symbol>
<symbol>
<name>$PUT</name>
<eval>"PUT"</eval>
</symbol>
<conditionalSymbol>
<name>$sessionAttribute</name>
<if condition="$httpServletRequest.getSession(false) != null>
<if condition="$httpServletRequest.getSession(false).getAttribute($GET) != null">
<return>$httpServletRequest.getSession(false).getAttribute($GET)</return>
</if>
<if condition="true">
<return>$httpServletRequest.getSession(false).getAttribute($PUT)</return>
</if>
</if>
</conditionalSymbol>
This symbol is assumed to be a part of the servlet request mapper. First, it checks whether an
HTTP session exists for the servlet; if not, the symbol returns null. If a session
is present, the symbol checks whether the servlet has a GET attribute, it returns the value of that
attribute. Otherwise, it returns the value of the PUT attribute. The second condition expression is
true; this value is used as an else clause. If the first condition is true, the request mapper does
not evaluate any further expressions; otherwise, it continues to the second expression.