Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

JSTL <c:if ....

Thread view: 
Tim Slattery - 22 May 2007 20:10 GMT
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.

Signature

Tim Slattery
Slattery_T@bls.gov
http://members.cox.net/slatteryt

Tim Slattery - 25 May 2007 14:09 GMT
>I have an object (ContactModel). One of its methods is named
>"isEmailAddressNull" and returns a boolean. I'm trying to query that
[quoted text clipped - 13 lines]
>
>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:otherwise>
     stuff I want to appear when email address is non-null
 </c:otherwise>
</c:choose>

It works fine. I don't get it.

Signature

Tim Slattery
Slattery_T@bls.gov
http://members.cox.net/slatteryt

Lew - 25 May 2007 15:03 GMT
>> I have an object (ContactModel). One of its methods is named
>> "isEmailAddressNull" and returns a boolean. I'm trying to query that
[quoted text clipped - 26 lines]
>
> 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.

Signature

Lew

Lew - 25 May 2007 15:48 GMT
> There's another syntax that, assuming there is an attribute
> 'emailAddress' in ContactBean, avoids duplicating information in the
[quoted text clipped - 4 lines]
>
> 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;
    }
}
-----------

</sscce>

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.