JavaEE/JSTL

Last-modified: 2013-01-06 (日) 23:36:28

使用例

tablib宣言

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

ifとforEach

    <c:if test="${pageCount > 0}">
      <c:forEach var="p" begin="0" end="${pageCount-1}">
        <c:choose>
          <c:when test="${p != page}">
            <a href = "?page=${p}">${p + 1}</a>
          </c:when>
          <c:otherwise>
            ${p+1}
          </c:otherwise>
        </c:choose>

      </c:forEach>
    </c:if>

    <table>
      <tr>
        <th>ID</th>
        <th>氏名</th>
        <th>備考</th>
      </tr>

      <c:forEach var="user" items="${userList}">
        <tr>
          <td>${fn:escapeXml(user.id)}</td>
          <td>${fn:escapeXml(user.name)}</td>
          <td><pre>${fn:escapeXml(user.remark)}</pre></td>
        </tr>
      </c:forEach>
    </table>

empty

    <c:if test="${! empty errorList}">
      <%-- errorList==nullの場合だけでなく errorList.size()==0の場合も
           empty errorList == trueとなるようだ。たまたま?仕様?要確認。 --%>
      <ul>
        <c:forEach var="error" items="${errorList}">
          <li>${fn:escapeXml(error)}</li>
        </c:forEach>
      </ul>
    </c:if>

リストの要素数の取得

fn:length を使うらしい。