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/05/15 08:15:32 UTC

IF / ELSE and SWITCH/CASE Tags

Can I submit these logic tags for inclusion in Struts - they are on the TODO
list for 1.1?

Attached are tags to do If/Else and Switch/Case logic, based on existing
Struts logic tag classes - the key classes inherit from the Struts
CompareTagBase.

1) IF/ELSE: (IfTag, ThenTag, ElseTag)

The IfTag provides the same functionality as the Equal, NotEqual, LessEqual,
LessThan, GreaterThan, GreaterEqual, Match, NoMatch, Present, NotPresent
tags by specifying that in the "op" attribute.

Example Usage:

  <logic:if op="GreaterThan" name="testbean" property="doubleProperty"
value="400">
      <logic:then>
            Property Greater Than Value
      </logic:then>
      <logic:else>
            Property Not Greater Than Value
      </logic:else>
  </logic:if>


2) SWITCH/CASE: (SwitchTag, CaseTag, DefaultTag)

Example Usage:

  <logic:switch name="testbean" property="doubleProperty">
    <logic:case value ="1">1 matched</logic:case>
    <logic:case value ="321">321 matched 1st</logic:case>
    <logic:case value ="321">321 matched 2nd</logic:case>
    <logic:case value ="555">555 matched</logic:case>
    <logic:default>No values matched - default processing</logic:default>
  </logic:switch>

[[ LOGIC.ZIP : 1568 in winmail.dat ]]

Re: IF / ELSE and SWITCH/CASE Tags

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Tue, 15 May 2001, Johan Compagner wrote:

> Great idee's!!

Sounds good to me.  There is also progress on conditional tags in the
Standard Tag Libraries effort that may or may not go in a similar
direction -- but that shouldn't stop us from building on the existing
concepts in the mean time.

> First thing to be included after the final of 1.0??
> 

Also sounds good -- I'm going to get 1.0-beta-2 out tonight no matter how
little sleep I end up with .... (Tomcat fires took precedence,
unfortunately).

> johan
> 
Craig


> 
> ----- Original Message -----
> From: "Niall Pemberton" <ni...@btInternet.com>
> To: <st...@jakarta.apache.org>
> Sent: Tuesday, May 15, 2001 8:15 AM
> Subject: IF / ELSE and SWITCH/CASE Tags
> 
> 
> > Can I submit these logic tags for inclusion in Struts - they are on the
> TODO
> > list for 1.1?
> >
> > Attached are tags to do If/Else and Switch/Case logic, based on existing
> > Struts logic tag classes - the key classes inherit from the Struts
> > CompareTagBase.
> >
> > 1) IF/ELSE: (IfTag, ThenTag, ElseTag)
> >
> > The IfTag provides the same functionality as the Equal, NotEqual,
> LessEqual,
> > LessThan, GreaterThan, GreaterEqual, Match, NoMatch, Present, NotPresent
> > tags by specifying that in the "op" attribute.
> >
> > Example Usage:
> >
> >   <logic:if op="GreaterThan" name="testbean" property="doubleProperty"
> > value="400">
> >       <logic:then>
> >             Property Greater Than Value
> >       </logic:then>
> >       <logic:else>
> >             Property Not Greater Than Value
> >       </logic:else>
> >   </logic:if>
> >
> >
> > 2) SWITCH/CASE: (SwitchTag, CaseTag, DefaultTag)
> >
> > Example Usage:
> >
> >   <logic:switch name="testbean" property="doubleProperty">
> >     <logic:case value ="1">1 matched</logic:case>
> >     <logic:case value ="321">321 matched 1st</logic:case>
> >     <logic:case value ="321">321 matched 2nd</logic:case>
> >     <logic:case value ="555">555 matched</logic:case>
> >     <logic:default>No values matched - default processing</logic:default>
> >   </logic:switch>
> >
> > [[ LOGIC.ZIP : 1568 in winmail.dat ]]
> >
> 
> 


Re: IF / ELSE and SWITCH/CASE Tags

Posted by Johan Compagner <jc...@j-com.nl>.
Great idee's!!
First thing to be included after the final of 1.0??

johan


----- Original Message -----
From: "Niall Pemberton" <ni...@btInternet.com>
To: <st...@jakarta.apache.org>
Sent: Tuesday, May 15, 2001 8:15 AM
Subject: IF / ELSE and SWITCH/CASE Tags


> Can I submit these logic tags for inclusion in Struts - they are on the
TODO
> list for 1.1?
>
> Attached are tags to do If/Else and Switch/Case logic, based on existing
> Struts logic tag classes - the key classes inherit from the Struts
> CompareTagBase.
>
> 1) IF/ELSE: (IfTag, ThenTag, ElseTag)
>
> The IfTag provides the same functionality as the Equal, NotEqual,
LessEqual,
> LessThan, GreaterThan, GreaterEqual, Match, NoMatch, Present, NotPresent
> tags by specifying that in the "op" attribute.
>
> Example Usage:
>
>   <logic:if op="GreaterThan" name="testbean" property="doubleProperty"
> value="400">
>       <logic:then>
>             Property Greater Than Value
>       </logic:then>
>       <logic:else>
>             Property Not Greater Than Value
>       </logic:else>
>   </logic:if>
>
>
> 2) SWITCH/CASE: (SwitchTag, CaseTag, DefaultTag)
>
> Example Usage:
>
>   <logic:switch name="testbean" property="doubleProperty">
>     <logic:case value ="1">1 matched</logic:case>
>     <logic:case value ="321">321 matched 1st</logic:case>
>     <logic:case value ="321">321 matched 2nd</logic:case>
>     <logic:case value ="555">555 matched</logic:case>
>     <logic:default>No values matched - default processing</logic:default>
>   </logic:switch>
>
> [[ LOGIC.ZIP : 1568 in winmail.dat ]]
>


RE: IF / ELSE and SWITCH/CASE Tags

Posted by Niall Pemberton <ni...@btInternet.com>.
Thanks for your input, I've tried to address what you raise below under your
points.

Niall

> -----Original Message-----
> From: Michael Hackett [mailto:mhackett@pictorius.com]
> Sent: 15 May 2001 16:41
> To: struts-dev@jakarta.apache.org
> Subject: RE: IF / ELSE and SWITCH/CASE Tags
>
>
> > 1) IF/ELSE: (IfTag, ThenTag, ElseTag)
> >
> > The IfTag provides the same functionality as the Equal, NotEqual,
> > LessEqual, LessThan, GreaterThan, GreaterEqual, Match, NoMatch,
> > Present, NotPresent tags by specifying that in the "op" attribute.
>
> Why not just continue to use the existing tags for the initial test, and
> only add an ELSE tag (used in the same way) to handle the alternate
> case? Or, if a symmetrical solution is preferred, one could still use
> the THEN tags inside the existing logic test tags.

1) I like If/Else it has a familiar ring ;-) and people get it, no
explanation Equal/Else Match/Else not quite the same.

2) It was easy to create an new set of tags - testimony to the great design
of the logic tags, I just basically put a new wrapper round the exsiting
Struts functionality.

3) The IfTag works differently to the existing logic tags. It just evaluates
the condition but always processes the tags body content. The embedded
Then/Else tags check with the IfTag on whether they need to either ignore
their content or process it. To have Then/Else with the existing tags would
probably require them all to work in two different ways - as they do
currently for backward compatibility and as my IfTag does - making those 10
tags more complex, plus both my Then/Else tags would have to check if they
are embedded in one of 10 tags rather than just the one IfTag. Additionally
I am not a Struts committer and can't make the decision to change those tags
(I wouldn't anyway).

4) Only adding an Else tag means you would have to parse the tags content
and determine which bit was part of the If and which part of the else and
then modify the content so you only have the bit you want. Haven't done this
and even if its possible makes it much more complex.

>
> However, I would actually like to see something more flexible, including
> the addition of AND and OR tags for wrapping a set of conditions. Maybe
> something like:
>
> <logic:conditional>
>   <logic:and>
>     <logic:equal name="bean" property="myProp" value="1"/>
>     ...
>   </logic:and>
>   <logic:onTrue>
>     <bean:write ... >
>   </logic:onTrue>
>   <logic:onFalse>
>     ...
>   </logic:onFalse>
> </logic:conditional>
>
> For the simple case, the <logic:and> might be replaced with a
> <logic:test>, or perhaps the test tag can simply be used in its place,
> the AND and OR simply being special case tests with bodies that contain
> more tests. If this could be made compatible with the existing system,
> without making the new functionality too ugly to use, that would be even
> better.

You can of course do AND processing with the existing tags or my IfTag:
   <logic:equal .....>
      <logic:equal .....>
      </logic:equal>
   </logic:equal>

I agree AND/OR with ELSE might be interesting to add - I just addressed the
"Else" issue which many people have raised in the past and is on the 1.1
ToDo list. However, I haven't had a need for it and part of me thinks this
would be putting the Model in the View, which I'm staying away from.

Anyway, you have the source, so if you want to go and try out your ideas I'd
be interested to see how you would implement them.

>
> I'm not set on any of the tag names; I just wanted to make clear their
> purposes. Suggestions for improvements are welcomed.
>
> --
> Michael Hackett
> Developer, Pictorius Inc.
>
>


RE: IF / ELSE and SWITCH/CASE Tags

Posted by Michael Hackett <mh...@pictorius.com>.
> 1) IF/ELSE: (IfTag, ThenTag, ElseTag)
>
> The IfTag provides the same functionality as the Equal, NotEqual,
> LessEqual, LessThan, GreaterThan, GreaterEqual, Match, NoMatch,
> Present, NotPresent tags by specifying that in the "op" attribute.

Why not just continue to use the existing tags for the initial test, and
only add an ELSE tag (used in the same way) to handle the alternate
case? Or, if a symmetrical solution is preferred, one could still use
the THEN tags inside the existing logic test tags.

However, I would actually like to see something more flexible, including
the addition of AND and OR tags for wrapping a set of conditions. Maybe
something like:

<logic:conditional>
  <logic:and>
    <logic:equal name="bean" property="myProp" value="1"/>
    ...
  </logic:and>
  <logic:onTrue>
    <bean:write ... >
  </logic:onTrue>
  <logic:onFalse>
    ...
  </logic:onFalse>
</logic:conditional>

For the simple case, the <logic:and> might be replaced with a
<logic:test>, or perhaps the test tag can simply be used in its place,
the AND and OR simply being special case tests with bodies that contain
more tests. If this could be made compatible with the existing system,
without making the new functionality too ugly to use, that would be even
better.

I'm not set on any of the tag names; I just wanted to make clear their
purposes. Suggestions for improvements are welcomed.

--
Michael Hackett
Developer, Pictorius Inc.