23

A quick JSTL question. I usually use scriptlets in my jsp pages, but have a conflict due to some other things in my page. I understand you can do something like this using JSTL, although I am not familiar with it. Here is what I would code using java for this:

if (var1.equalsIgnoreCase(var2)) { 

some html stuff

} else {

more html

}

So can this be converted and translated to be used with JSTL?

Thanks in advance and if you have any questions, just let me know.

AGS
  • 14,288
  • 5
  • 52
  • 67
Mr Man
  • 1,498
  • 10
  • 33
  • 54

2 Answers2

56

You can use <c:choose> for this. The equalsIgnoreCase() can be done by lowercasing the both sides by fn:toLowerCase().

<c:choose>
    <c:when test="${fn:toLowerCase(var1) == fn:toLowerCase(var2)}">
        Both are equal.
    </c:when>
    <c:otherwise>
        Both are not equal.
    </c:otherwise>
</c:choose>

Or when you're targeting a Servlet 3.0 container (Tomcat 7, Glassfish 3, JBoss AS 6, etc) with a web.xml declared conform Servlet 3.0, then you can invoke the equalsIgnoreCase() method.

<c:choose>
    <c:when test="${var1.equalsIgnoreCase(var2)}">
        Both are equal.
    </c:when>
    <c:otherwise>
        Both are not equal.
    </c:otherwise>
</c:choose>
Ajinkya
  • 22,324
  • 33
  • 110
  • 161
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • +1 what I was writing. The lack of a direct if/else is a commonly-complained-about feature of JSTL. choose/when/otherwise is probably better than two ifs with the condition inverted, but it sure is wordy. Then again this *is* Java after all! – bobince Jun 02 '11 at 19:22
  • @bobince: you can indeed also go for two `` tags wherein the other compares the opposite. – BalusC Jun 02 '11 at 19:23
  • @BalusC , this makes sense, though I am not very familiar with JSTL, and I am getting a java.lang.ClassCastException. Both var1 and var2 are strings, if that matters. I have imported the taglib as well. Any reason I might be getting this? – Mr Man Jun 02 '11 at 19:34
  • You're getting a CCE on the `.equalsIgnoreCase()`? Well, apparently they're EL-coerced to a different type while setting the variables. I'd just go for the first approach. Add `<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>` to top to get `fn:xxx` functions to work. – BalusC Jun 02 '11 at 19:35
  • I still dont know... I'm getting errors on adding that new taglib now – Mr Man Jun 02 '11 at 20:06
  • Please share the errors. They contain the answer. It's just how you interpret (or ignore) them. At any way, in our JSTL wiki page you can find some background information about getting JSTL to work: http://stackoverflow.com/tags/jstl/info – BalusC Jun 02 '11 at 20:06
  • I will take a look at that page, I got it working as long as I just use var1 == var2. Thanks! – Mr Man Jun 02 '11 at 20:10
  • OK, that the `c:` tags work while `fn:` doesn't indicates a messup in the classpath. Please carefully read the JSTL wiki page to get it straight (i.e. which JARs and configs you *really* need or not, e.g. for JSTL 1.2 you should *not* have a `standard.jar` and on). – BalusC Jun 02 '11 at 20:13
  • +1 for sharing Servlet 3.0 thing, didn't knew about that. – Abhinav Dec 10 '13 at 20:37
  • thank you ,when i want using `c:if` and `c:else` but jstl not hava `c:else`...you answer worked – Vito Jun 17 '14 at 02:41
2
<c:if test=${var1 == var2)}>
</c:if>

There is no Else is JSTL you have to do multiple If's (Sucks I know)

Must add this at top

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
RMT
  • 7,040
  • 4
  • 25
  • 37