Subject: Contribution: 1) IF, AND, OR, THEN, ELSE, ELSEIF tags 2) SWITCH, CASE, DEFAULT tags
Date: Sun, 24 Jun 2001 21:27:57 +0100
From: "Niall Pemberton" <niall.pemberton@btInternet.com>
I have extended the IF, THEN, ELSE functionality I posted a while ago to now include AND, OR and ELSEIF tags. I have also added an enhacement to the original CASE tag which now allows an operator as well as value to be specified.
I attach the source code for these tags and would like to propose them to be considered for inclusion in Struts. If you agree with including them in Struts, then send some feedback to the development list (struts-dev@jakarta.apache.org).
1) Tags which make up the IF/ELSE logic are:
IfTag
AndTag
OrTag
ThenTag
ElseTag
ElseifTag
2) Tags which make up the SWITCH/CASE logic are:
SwitchTag
CaseTag
DefaultTag
These tags use the same Struts base tags which the existing logic tags use and are effectively just new "wrappers" to provide IF/ELSE and SWITCH/CASE functionality.
The IfTag, AndTag, OrTag and ElseifTag provides the same functionality as the existing Struts logic tags (e.g. EqualTag etc) by specifying an operator (e.g. op="equal") attribute. Valid operator values are: Equal, NotEqual, LessEqual, LessThan, GreaterThan, GreaterEqual, Match. NoMatch, Present, NotPresent
The CaseTag also uses an operator attribute and can have the following values: Equal, NotEqual, LessEqual, LessThan, GreaterThan, GreaterEqual
Its obvious that trying to specify complex conditional logic in an XML format is never going to have the same clarity as a programming language, so I hope you bear that in mind when seeing how these tags achieve that.
Using the IF/ELSE TAGS
-------------------------------------
I have included a few examples below to demonstrate how to use the tags and I also try document the key points when laying out these tags:
How the tags are ordered and embedded in each other is important:
1) The first tag must be an IfTag which encloses all other tags
2) AndTags and OrTags must be embedded after an IfTag or ElseIfTag and
before the ThenTag and ElseTag.
3) AndTags can only have OrTags embedded in them
4) OrTags can only have AndTags embedded in them
5) You can't mix and match AndTags and OrTags at the SAME level - must be
all AndTags or all OrTags
Niall
******************************************************
Example 1 - IF, OR, THEN, ELSE
<logic:if name="calendarBean" property="month" op="equal"
value="January" >
<logic:or name="calendarBean" property="year" op="LessEqual"
value="1999"/>
<logic:or name="calendarBean" property="day" op="equal"
value="Monday"/>
<logic:then>
At least one of the above conditions is true.
</logic:then>
<logic:else>
All of Above conditions are false
</logic:else>
</logic:if>
******************************************************
Example 2 - IF, AND, OR, THEN, ELSE, ELSEIF
<logic:if name="personBean" op="present">
<logic:then>
No details found for this Person
</logic:then>
<logic:elseif name="personBean" property="age" op="GreaterEqual"
value="16">
<logic:and name="personBean" property="age" op="LessEqual"
value="65">
<logic:or name="personBean" property="age" op="LessEqual"
value="75">
<logic:and name="personBean" property="job" op="equal"
value="Doctor">
<logic:or name="personBean" property="job" op="equal"
value="Dentist"/>
<logic:or name="personBean" property="job" op="equal"
value="Lawyer"/>
</logic:and>
</logic:or>
<logic:or name="personBean" property="job" op="equal"
value="President"/>
<logic:or name="personBean" property="job" op="equal"
value="Senator"/>
</logic:and>
<logic:then>
This person is of working age - that is between the ages of 16
and 65,
except for Doctors, Dentists and Lawyers who retire at 55
and presidents and senators who can be any age over 16.
</logic:then>
<logic:else>
This person is either too young to work or past retirement age.
</logic:else>
</logic:elseif>
</logic:if>
******************************************************
Example 3 - - IF, OR, THEN, ELSE, ELSEIF
<logic:if name="calendarBean" property="month" op="equal"
value="January" >
<logic:or name="calendarBean" property="month" op="equal"
value="February"/>
<logic:or name="calendarBean" property="month" op="equal"
value="March"/>
<logic:then>
This if the first quarter of the year.
</logic:then>
<logic:elseif name="calendarBean" property="month" op="equal"
value="April">
<logic:or name="calendarBean" property="month" op="equal"
value="May"/>
<logic:or name="calendarBean" property="month" op="equal"
value="June"/>
<logic:then>
This if the second quarter of the year.
</logic:then>
<logic:elseif name="calendarBean" property="month" op="equal"
value="July">
<logic:or name="calendarBean" property="month" op="equal"
value="August"/>
<logic:or name="calendarBean" property="month" op="equal"
value="September"/>
<logic:then>
This if the third quarter of the year.
</logic:then>
<logic:else>
This if the fourth quarter of the year.
</logic:else>
</logic:elseif>
</logic:elseif>
</logic:if>
******************************************************
Example 4 - SWITCH, CASE, DEFAULT
<logic:switch name="testbean" property="doubleProperty">
<logic:case op="Equal" value="1">Value is 1</logic:case>
<logic:case op="LessEqual" value="10">Value is 2 to 10</logic:case>
<logic:case op="GreaterThan" value="100">Value is 11 to
100</logic:case>
<logic:case op="GreaterThan" value="1000">Value is greater than
1000</logic:case>
<logic:default>Value is 101 to 1000</logic:default>
</logic:switch>