JSTL <c:if ....

T

Tim Slattery

I have an object (ContactModel). One of its methods is named
"isEmailAddressNull" and returns a boolean. I'm trying to query that
field from a <c:if ... tag in a JSP page. I get no error messages, but
I get the wrong values. The relevant part of the page looks like this:

<c:if test="${!(ContactBean.emailAddressNull)}">
<a id="UpdateEmail" href="/CESLite/Email.do"
class="menu" title="Update Email Address">Update Email
Address</a>&nbsp;|&nbsp;
</c:if>

Coded this way, the link *always* appears, regardless of the value
returned by isEmailAddressNull. If I change it to:

<c:if test="${ContactBean.eMailAddressNull == false}">

then the link *never* appears, regardless of the value returned.
 
T

Tim Slattery

Tim Slattery said:
I have an object (ContactModel). One of its methods is named
"isEmailAddressNull" and returns a boolean. I'm trying to query that
field from a <c:if ... tag in a JSP page. I get no error messages, but
I get the wrong values. The relevant part of the page looks like this:

<c:if test="${!(ContactBean.emailAddressNull)}">
<a id="UpdateEmail" href="/CESLite/Email.do"
class="menu" title="Update Email Address">Update Email
Address</a>&nbsp;|&nbsp;
</c:if>

Coded this way, the link *always* appears, regardless of the value
returned by isEmailAddressNull. If I change it to:

<c:if test="${ContactBean.eMailAddressNull == false}">

then the link *never* appears, regardless of the value returned.

Addendum: I guess this has to do with negating the value I'm
retrieving. When I use a <c:choose> group like this:

<c:choose>
<c:when test="${ContactBean.emailAddressNull}">
</c:when>
<c:eek:therwise>
stuff I want to appear when email address is non-null
</c:eek:therwise>
</c:choose>

It works fine. I don't get it.
 
L

Lew

Tim said:
Addendum: I guess this has to do with negating the value I'm
retrieving. When I use a <c:choose> group like this:

<c:choose>
<c:when test="${ContactBean.emailAddressNull}">
</c:when>
<c:eek:therwise>
stuff I want to appear when email address is non-null
</c:eek:therwise>
</c:choose>

It works fine. I don't get it.

I don't either, assuming the boolean test worked the same both times and that
the parentheses didn't matter. Have you tested with equivalent constants?

<c:if test="${! true }" >

There's another syntax that, assuming there is an attribute 'emailAddress' in
ContactBean, avoids duplicating information in the bean (maintaining both the
address and the flag copying the state) and eliminates the redundant boolean
is...() method:

<c:if test="${! empty ContactBean.emailAddress}" >

which has the virtue of handling the value being null or empty.
 
L

Lew

Lew said:
There's another syntax that, assuming there is an attribute
'emailAddress' in ContactBean, avoids duplicating information in the
bean (maintaining both the address and the flag copying the state) and
eliminates the redundant boolean is...() method:

<c:if test="${! empty ContactBean.emailAddress}" >

which has the virtue of handling the value being null or empty.

I tried the following and found negation worked just fine:

<sscce exceptfor="not showing the rest of the Web app infrastructure such as
web.xml" >

checkit.jsp
-----------
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Negation Test</h1>

<jsp:useBean id="eg" class="testit.Example" />
<jsp:setProperty name="eg" property="name" value="Jo" />

<c:if test="${ eg.non }">
<h2>Yes non</h2>
</c:if>
<c:if test="${ ! eg.non }">
<h2>Not non</h2>
</c:if>
<c:if test="${ ! (eg.non) }">
<h2>Not (non)</h2>
</c:if>
<c:if test="${ ! empty eg.name }">
<h2>Not empty name</h2>
</c:if>
</body>
</html>
-----------

testit/Example.java
-----------
package testit;

public class Example
{
private String name;

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

public boolean isNon()
{
return name == null;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top