You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Niall Pemberton <ni...@btInternet.com> on 2001/06/24 22:27:57 UTC
Contribution: 1) IF, AND, OR, THEN, ELSE, ELSEIF tags 2) SWITCH, CASE, DEFAULT tags
Hi,
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
[[ AMARDA~1.TLD : 5177 in winmail.dat ]][[ LOGIC-~1.JAR : 5178 in
winmail.dat ]]
******************************************************
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>
RE: Contribution: 1) IF, AND, OR, THEN, ELSE, ELSEIF tags 2) SWITCH, CASE, DEFAULT tags
Posted by DHarty <ha...@db.erau.edu>.
I agree.
Add to that the fact that java script functionality is not consistent across
all browsers, and some organizations refuse to allow its use in intranet
applications.
D
-----Original Message-----
From: bram [mailto:bram@info.nl]
Sent: Monday, June 25, 2001 5:46 AM
To: struts-user@jakarta.apache.org
Subject: Re: Contribution: 1) IF, AND, OR, THEN, ELSE, ELSEIF tags 2)
SWITCH, CASE, DEFAULT tags
But tags have one main advantage, the "view" builder doesn't have to
understand Java and it is easy to intergrate the tags in a wysiwyg jsp
editor. We use that kind of tags to make our webpages, eg see jsp
and all we have to do is to make a xml based editor...
with scriplets this is impossible.
***** begin jsp ****
<template:insert template="/document/html.homepage.jsp">
<template:put name="banner"
content="/element/banner/html.standard.jsp"/>
<wf:choose>
<wf:whenUser level="2">
<template:put name="leftbar"
content="/element/leftbar/html.standard_level2.jsp" />
</wf:whenUser>
<wf:whenUser level="1">
<template:put name="leftbar"
content="/element/leftbar/html.standard_level1.jsp" />
</wf:whenUser>
<wf:otherwise>
<template:put name="leftbar"
content="/element/leftbar/html.standard.jsp" />
</wf:otherwise>
</wf:choose>
<template:put name="content"
content="/element/content/html.homepage.jsp"/>
<template:put name="menu" content="/element/menu/html.homepage.jsp"/>
</template:insert>
***** end jsp ****
----- Original Message -----
From: "Wong Kok Wai" <wo...@yahoo.com>
To: <st...@jakarta.apache.org>
Sent: Monday, June 25, 2001 7:41 AM
Subject: Re: Contribution: 1) IF, AND, OR, THEN, ELSE, ELSEIF tags 2)
SWITCH, CASE, DEFAULT tags
> No offense, but I feel it is much better in
> performance to use Java scriptlets for these cases.
> The overhead of the tags is just too high to justify
> using them.
>
>
>
> __________________________________________________
> Do You Yahoo!?
> Get personalized email addresses from Yahoo! Mail
> http://personal.mail.yahoo.com/
>
Re: Contribution: 1) IF, AND, OR, THEN, ELSE, ELSEIF tags 2) SWITCH, CASE, DEFAULT tags
Posted by bram <br...@info.nl>.
But tags have one main advantage, the "view" builder doesn't have to
understand Java and it is easy to intergrate the tags in a wysiwyg jsp
editor. We use that kind of tags to make our webpages, eg see jsp
and all we have to do is to make a xml based editor...
with scriplets this is impossible.
***** begin jsp ****
<template:insert template="/document/html.homepage.jsp">
<template:put name="banner"
content="/element/banner/html.standard.jsp"/>
<wf:choose>
<wf:whenUser level="2">
<template:put name="leftbar"
content="/element/leftbar/html.standard_level2.jsp" />
</wf:whenUser>
<wf:whenUser level="1">
<template:put name="leftbar"
content="/element/leftbar/html.standard_level1.jsp" />
</wf:whenUser>
<wf:otherwise>
<template:put name="leftbar"
content="/element/leftbar/html.standard.jsp" />
</wf:otherwise>
</wf:choose>
<template:put name="content"
content="/element/content/html.homepage.jsp"/>
<template:put name="menu" content="/element/menu/html.homepage.jsp"/>
</template:insert>
***** end jsp ****
----- Original Message -----
From: "Wong Kok Wai" <wo...@yahoo.com>
To: <st...@jakarta.apache.org>
Sent: Monday, June 25, 2001 7:41 AM
Subject: Re: Contribution: 1) IF, AND, OR, THEN, ELSE, ELSEIF tags 2)
SWITCH, CASE, DEFAULT tags
> No offense, but I feel it is much better in
> performance to use Java scriptlets for these cases.
> The overhead of the tags is just too high to justify
> using them.
>
>
>
> __________________________________________________
> Do You Yahoo!?
> Get personalized email addresses from Yahoo! Mail
> http://personal.mail.yahoo.com/
>
Re: Contribution: 1) IF, AND, OR, THEN, ELSE, ELSEIF tags 2)
SWITCH, CASE, DEFAULT tags
Posted by "Craig R. McClanahan" <cr...@apache.org>.
On Sun, 24 Jun 2001, Wong Kok Wai wrote:
> No offense, but I feel it is much better in
> performance to use Java scriptlets for these cases.
> The overhead of the tags is just too high to justify
> using them.
>
Performance is one of the reasons the JSP Standard Tag Libraries effort
was started (JSR-052 in the Java Community Process). Once a set of tags
are standardized, the JSP page compiler can be made smart enough to create
optimized code to implement them (just as it already understands what
<jsp:useBean> means).
In particular, conditionals and iteration are going to be included in the
first versions of the standard tag library (early access "soon"). So,
it's probably not worth huge amounts of effort to extend the logic tags in
Struts, if the standard versions of the tags will meet our needs.
Craig McClanahan
RE: Contribution: 1) IF, AND, OR, THEN, ELSE, ELSEIF tags 2) SWITCH, CASE, DEFAULT tags
Posted by Wong Kok Wai <wo...@yahoo.com>.
Just off the top of my head:
First, the tag has to be instantiated, then the
methods in the Tag interface are called. These already
are more than a simple "if" instruction. Of course, if
the JSP compiler can recoqnise these tags and
optimises the code generation, then the performance
issue will go away.
--- Niall Pemberton <ni...@btInternet.com>
wrote:
> None taken.
>
> I dont have a feel for the overhead - could you
> expand more.
>
> Niall
>
__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/
RE: Contribution: 1) IF, AND, OR, THEN, ELSE, ELSEIF tags 2) SWITCH, CASE, DEFAULT tags
Posted by Niall Pemberton <ni...@btInternet.com>.
None taken.
I dont have a feel for the overhead - could you expand more.
Niall
> -----Original Message-----
> From: Wong Kok Wai [mailto:wongkokwai@yahoo.com]
> Sent: 25 June 2001 06:41
> To: struts-user@jakarta.apache.org
> Subject: Re: Contribution: 1) IF, AND, OR, THEN, ELSE, ELSEIF tags 2)
> SWITCH, CASE, DEFAULT tags
>
>
> No offense, but I feel it is much better in
> performance to use Java scriptlets for these cases.
> The overhead of the tags is just too high to justify
> using them.
>
>
>
> __________________________________________________
> Do You Yahoo!?
> Get personalized email addresses from Yahoo! Mail
> http://personal.mail.yahoo.com/
>
Re: Contribution: 1) IF, AND, OR, THEN, ELSE, ELSEIF tags 2) SWITCH, CASE, DEFAULT tags
Posted by Wong Kok Wai <wo...@yahoo.com>.
No offense, but I feel it is much better in
performance to use Java scriptlets for these cases.
The overhead of the tags is just too high to justify
using them.
__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/