3

I am well aware of how to build an "if(confition) ... else ..." statment using JSTL and the <choose>, <when> and <otherwise> tag.

However I would be interested to know the design decision(s) that lead to force this slightly heavy and verbose construction on developpers even for simple if-else statements and not to include a tag that would work together with <s:if> (though I think the choose tag is much more useful and elegant for multiple choices, kind of like a powerful switch(...) statement or something like that).

Struts2 has an <s:else> tag, so I don't think it was a technical impossibility...

If anyone can enlighten me I'd appreciate it.

Edit : and by the way, does anybody think that "otherwise" is really better thant "else" ? (I am no native english speaker so there might be different "shades of if" that I am not getting here - anyway I'd rather type "else" :-)

Community
  • 1
  • 1
Pierre Henry
  • 16,658
  • 22
  • 85
  • 105
  • 1
    choose..when..otherwise is more like switch..case than if..else – JKirchartz Mar 25 '13 at 20:05
  • 1
    not really, a switch...case statement usually only acts on a single atomic variable (number, string,...), not with arbitrary conditions. The choose...when...otherwise is more like an if...elseif...else with0 to n elseifs. – Pierre Henry Jun 12 '13 at 12:22

2 Answers2

0

Many of the JSTL elements seem (to me) to be based on XSLT. For example xsl:choose.

kevinjansz
  • 638
  • 6
  • 20
0

Generally IF statement has to be followed by ELSE statement and there is nothing else supposed to be done in between those comments.

We dont have because, had it been there, one could have added some other stuff between <c:if> .. </c:if> AND <c:else> .. </c:else> which is not supposed to be there.

To maintain the flow exactly IF ELSE and not violating the it, ELSE not provided.

But not a big deal, we do have other tags to get this done:

<c:choose>
  <c:when test="${condition1}">
    ...
  </c:when>
  <c:when test="${condition2}">
    ...
  </c:when>
  <c:otherwise>
    ...
  </c:otherwise>
</c:choose>

Reference : http://careerguide.techproceed.com/2013/11/jstl-tutorial-with-examples.html

Snehal Masne
  • 3,403
  • 3
  • 31
  • 51