JSTL <c:if ....

Discussion in 'Java' started by Tim Slattery, May 22, 2007.

  1. Tim Slattery

    Tim Slattery Guest

    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.

    --
    Tim Slattery

    http://members.cox.net/slatteryt
     
    Tim Slattery, May 22, 2007
    #1
    1. Advertising

  2. Tim Slattery

    Tim Slattery Guest

    Tim Slattery <> wrote:

    >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.

    --
    Tim Slattery

    http://members.cox.net/slatteryt
     
    Tim Slattery, May 25, 2007
    #2
    1. Advertising

  3. Tim Slattery

    Lew Guest

    Tim Slattery wrote:
    > Tim Slattery <> wrote:
    >
    >> 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.


    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.

    --
    Lew
     
    Lew, May 25, 2007
    #3
  4. Tim Slattery

    Lew Guest

    Lew wrote:
    > 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;
    }
    }
    -----------

    </sscce>

    --
    Lew
     
    Lew, May 25, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. steve

    JSTL to C# conversion?

    steve, Jun 1, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    369
    steve
    Jun 1, 2004
  2. Derek Mahar
    Replies:
    2
    Views:
    40,810
    DoggieDaddy
    Jun 7, 2011
  3. Bryan Althaus

    JSTL & WebSphere 5.0 setup possible?

    Bryan Althaus, Jul 22, 2003, in forum: Java
    Replies:
    0
    Views:
    1,067
    Bryan Althaus
    Jul 22, 2003
  4. Robert Mark Bram

    Custom JSTL that accepts JSTL expression

    Robert Mark Bram, Dec 15, 2004, in forum: Java
    Replies:
    0
    Views:
    1,018
    Robert Mark Bram
    Dec 15, 2004
  5. DaFoot
    Replies:
    1
    Views:
    1,775
    DaFoot
    Oct 23, 2008
Loading...

Share This Page