JSTL true and ‘true’ comparisons

This code shows that jstl is smart enough to converts strings to boolean values. Also proves that JSTL looks in all the scopes like ‘request, pageContext, session’. (Refer to my previous post for pageContext). JSTL is also smart enough to compare numbers and strings. Here is the code:

Document   : JSTL true string comparision and JSTL tag scopes
Created on : Sep 12, 2008, 3:09:31 PM
Author     : gundlapa

<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”

<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>JSP Page</title>
String trueString=”true”;
boolean trueBoolean=true;
request.setAttribute(“trueString”, trueString);
request.setAttribute(“trueBoolean”, java.lang.Boolean.TRUE);
String string1=”1″;
Integer int1=new Integer(1);
request.setAttribute(“string1″, string1);
request.setAttribute(“int1″, int1);
<c:if test=”${trueString}”>
The string is true<BR>
<c:if test=”${trueBoolean}”>
The boolean is true<BR>
<c:if test=”${trueString eq true}”>
The string is true<BR>
<c:if test=”${trueBoolean eq ‘true’}”>
The boolean is true<BR>
<c:if test=”${int1 eq ’1′}”>
<c:if test=”${string1 eq 1}”>
The output:

The string is true
The boolean is true
The string is true
The boolean is true



Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...