简体   繁体   中英

JSTL <c:if> tag - Checking whether Java Bean method returns True

I currently have a .jsp file as follows (partly shown):

<jsp:useBean id="cart" class="edu.unsw.comp9321.assignment1.CartBean"
scope="session" />  
<tr>
    <td><input type="submit" name="cartBtn" value="Go To Checkout"/></td>
    <td><input type="submit" name="cartBtn" value="Remove From Cart"/></td>
    <c:if test="${cart.isCartListEmpty eq true}"> 
        <td><input type="submit" name="cartBtn" value="Back To Search"/></td>
    </c:if> 
</tr>       

The bean cart has the code:

public boolean isCartListEmpty(){
    if (this.cart.isEmpty())
        return true;
    return false;
}

The problem is I am getting an error with the following line:

<c:if test="${cart.isCartListEmpty eq true}">

Could someone please tell me the correct way to evaluate whether the bean method is returning true or false ?

Thank you for your help.

The EL ${bean.attribute} will try to call the method getAttribute() or isAttribute() of the bean .

So in your case, ${cart.isCartListEmpty} will look for a method getIsCartListEmpty() or isIsCartListEmpty() in your cart .

What you need is thus ${cart.cartListEmpty} to call the appripriate isCartListEmpty() method.


Side notes:

  1. No need to add the eq true part since isCartListEmpty() already returns a boolean.
  2. You can (should?) simplify your isCartListEmpty() method body to return this.cart.isEmpty() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM