Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do the equivalent of a java If-Else block using JSTL? [duplicate]

Tags:

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.

like image 735
Dan Avatar asked Jun 02 '11 19:06

Dan


People also ask

What is use of JSTL in Java?

The JavaServer Pages Standard Tag Library (JSTL) is a collection of useful JSP tags which encapsulates the core functionality common to many JSP applications. JSTL has support for common, structural tasks such as iteration and conditionals, tags for manipulating XML documents, internationalization tags, and SQL tags.

Which of these is a valid JSTL if tag?

The < c:if > tag is used for testing the condition and it display the body content, if the expression evaluated is true. It is a simple conditional tag which is used for evaluating the body content, if the supplied condition is true.

What is JSTL in spring boot?

JavaServer Pages Tag Library (JSTL) is a set of tags that can be used for implementing some common operations such as looping, conditional formatting, and others. Here we will be discussing how to use the Maven build tool to add JSTL support to a Spring MVC application.

What is JSTL view?

public class JstlView extends InternalResourceView. Specialization of InternalResourceView for JSTL pages, i.e. JSP pages that use the JSP Standard Tag Library. Exposes JSTL-specific request attributes specifying locale and resource bundle for JSTL's formatting and message tags, using Spring's locale and MessageSource ...


1 Answers

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> 
like image 96
BalusC Avatar answered Oct 29 '22 04:10

BalusC