Ternary operator in JSTL/EL
I tested the following page in Tomcat 5.59, JSP 2.0 and JSTL 1.1. It ran without any errors.
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="value" scope="request" value="someValue"/>
<c:out default="None" escapeXml="true" value="${not empty value ? value : 'None'}" />
<c:out default="None" escapeXml="true" value="${empty value ? 'None' : value}" />
<c:set var="value" scope="request" value="" />
<br/>
<c:out default="None" escapeXml="true" value="${not empty value ? value : 'None'}" />
<c:out default="None" escapeXml="true" value="${empty value ? 'None' : value}" />
Related videos on Youtube
Tiny
Just an orphan kid and have no more to say. Three things in general, cannot be avoided (at least I can never) Mother Mother-tongue Mother-land. They are always unique. I'm a family-less boy. My family was hunted leaving me all alone when my house targeted and deliberately set on a fire by a mob during a nonsense communal riot but I was survived by a rescue team with the help of firemen. As a survival, I didn't know whether it was my fortune or misfortune but when I recovered, the rescue team came to my home, one day. One of the members gave me a piece of paper in my hand in which the following text was written. lifeisnowhere. He asked me to read it carefully and I could hardly interpret the text as Life is now here, instead of Life is nowhere. All of them gave me a cute smile and went away and I decided to live peacefully and hopefully on their saying from then onwards and very soon. Because of this tragedy, I'm alone couldn't join a school but a curiosity to learn something made me a self-learner. I'm indeed a self-learner, so I'm likely not able to answer any questions on this site right now. In the field of computer science, my self-study mainly includes, QBASIC, C, C++, C#, VB, Java, JavaScript, PHP and a little about ASP.NET. Oracle, MySQL and MSSQL-Server with DBMS. and other theoretical subjects. I'm currently dealing with - Android and Java EE including Servlet, JSP-JSTL/EL (with Spring and Struts with ORM models JPA/Hibernate) and JSF.
Updated on July 27, 2022Comments
-
Tiny almost 2 years
The following tag of JSTL can be used to set a value to a variable in a request scope.
<c:set var="value" scope="request" value="someValue"/>
I want to check conditionally, if the variable
value
being set is empty or not and display the result accordingly something like the following, using<c:when>...</c:when>
.<c:choose> <c:when test="${not empty value}"> <c:out default="None" value="${value}"/> </c:when> <c:otherwise> <c:out default="None" value="None"/> </c:otherwise> </c:choose>
I want to reduce the line of code using a ternary expression like,
<c:out default="None" value="${not empty value ? value : 'None'}"/>
It is evaluated as it actually means but if I interchange the order of the expressions like,
<c:out default="None" value="${empty value ? 'None' : value}"/>
then it is a syntax error indicating,
"${empty value?'None':value}" contains invalid expression(s): javax.el.ELException: Error Parsing: ${empty value?'None':value}
So why does this happen?
I'm using the
JSTL 1.1
library and the following taglib is included,<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
-
rickz over 11 yearsI copied and pasted your code and it works for me without error.
-
Tiny over 11 years@rickz - It might be the problem with the library version of JSTL I'm using. Which library are you using?
-
Med over 11 yearsThe issue encountered here seems to be related to the EL parser, not to the JSTL. Which version of the Servlet API are you running your app on? 3.0 ? 2.5 ? Older ?
-
Tiny over 11 years@Med - My Servlet version is 2.5.
-
-
Tiny over 11 yearsI have recently downloaded NetBeans 7.2.1 and jdk 7 and reconfigured the whole application I'm working with all over again from scratch. NetBeans 7.2.1 has Apache Tomcat 7.0.27.0 (previously it was 6.0.26.0 with NetBeans 6.9.1) which supports Servlet 3.0 (previously it was 2.5) where the problem in question disappeared. I didn't change the JSTL library. It is still JSTL 1.1 as before.