0

I'm developing web app using jsp.

What I want to do is, if a variable is not null, I'll display it, but if null, I'll display - character in table row.

This my code so far, I learned from here

<table id="hor-minimalist-a" summary="Employee Pay Sheet">
    <caption>Riwayat Status</caption>
        <thead>
            <tr>
             <th scope="col">Status</th>
             <th scope="col">Tanggal</th>

            </tr>
        </thead>
        <tbody>
            <tr>  
             <td>Tanggal DP</td>
             <c:choose>
               <c:when test="${Transaction.tglDP}">
                <td>${Transaction.tglDP}</td>
               </c:when>
               <c:otherwise}>
                <td>-</td>
               </c:otherwise>
             </c:choose>
</tr>
</tbody>
</table>

But when I run the website, when that variable is not null, it displaying the value and character - too.

What am I missing here?

Edit : for information, Transaction.tglDP type is java.util.Date

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
bangandi
  • 189
  • 1
  • 3
  • 13

2 Answers2

3

You can use not empty for null check, also you dont have to do multiple when if you want if else block just use otherwise

<c:choose>
      <c:when test="${not empty  Transaction.tglDP}">
            <td>${Transaction.tglDP}</td>
      </c:when>
      <c:otherwise>
            <td>-</td>
      </c:otherwise>
</c:choose>
mprabhat
  • 20,107
  • 7
  • 46
  • 63
  • i've tried your suggestion, but the displaying result is still the same, it's like two is displayed whether it's null or not. – bangandi Jun 04 '12 at 07:38
  • please post more code of yours either when or otherwise will get executed, both is not a possibility – mprabhat Jun 04 '12 at 08:00
  • my code is just like yours, what should i post? i'm sorry i don't understand your comment... – bangandi Jun 04 '12 at 08:06
  • Only one option will be executed out of c:when or c:otherwise both cannot be executed, so if you have more code in your jsp page, please post that – mprabhat Jun 04 '12 at 08:10
  • @bangandi: your code is not the same as mprabhat's code. Th syntax is ``, and not ``. – JB Nizet Jun 04 '12 at 08:24
  • @JBNizet i'm sorry, i forget to copy all of my updated code from netbeans. I was just adding addition code. Now it's all updated. The result is still the same – bangandi Jun 04 '12 at 11:36
  • You still have an additional curly bracket in c:otherwise. I'm surprised this even compiles. Look at the generated HTML code (using View page source in your browser). I suspect the JSTL core tag library isn't even declared in the JSP, and that you'll find in the generated HTML because the tags are not interpreted as JSP tags by the container. – JB Nizet Jun 04 '12 at 11:38
0

Finally i know what's wrong with my code. Because I'm new in jsp and the link from my question above didn't mention about using jstl library, i thought it was enough to just code like that. My own mistake. I didn't include the jstl.1.2.jar in my lib and forget to place this line :

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

And also view answer from @mprabhat . Thanks for everyone :)

bangandi
  • 189
  • 1
  • 3
  • 13