使用jsp </c:choose>在表中使用<c:choose>时出错

时间:2012-06-04 07:28:48

标签: jsp if-statement html-table

我正在使用jsp开发Web应用程序。

我想要做的是,如果变量不为null,我将显示它,但如果为null,我将在表行中显示-个字符。

到目前为止我的代码是从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>

但是当我运行网站时,当该变量不为null时,它也会显示值和字符。

我在这里缺少什么?

编辑:有关信息,Transaction.tglDP类型为java.util.Date

2 个答案:

答案 0 :(得分:3)

你可以使用not empty进行空检查,如果你想要if else阻止只是使用,你也不必做多次

<c:choose>
      <c:when test="${not empty  Transaction.tglDP}">
            <td>${Transaction.tglDP}</td>
      </c:when>
      <c:otherwise>
            <td>-</td>
      </c:otherwise>
</c:choose>

答案 1 :(得分:0)

最后我知道我的代码有什么问题。因为我是jsp的新手,而且我上面提到的问题的链接没有提到使用jstl库,我认为这样就足够了。我自己的错误。 我没有在我的lib中包含jstl.1.2.jar而忘记放置这一行:

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

还可以查看@mprabhat的回答。 谢谢大家:)