JSP/JSTL

      在〈JSP/JSTL〉中尚無留言

JSP (Jakarta Server Page) 是早期的技術,因結構鬆散維護不易,所以才有 Servlet 橫空出世。但 Servlet 以 JSP 為石,所以理解 JSP 是必需的。

變數

${variance}

JSTL

先下載 jakarta.servlet.jsp.jstl-3.0.1.jarjakarta.servlet.jsp.jstl-api-3.0.2.jar ,置於 Libraries 之下,然後於 jsp 檔案引入 JSTL (JSP Standard Tag Library)

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

if

於 jsp 檔案使用如下 tag

<c:if test="${not empyt userAccount}">
<p>
${userAccount}你好
</p>
</c:if>

if/else

if/else 的語法如下

<c:choose>
    <c:when test="${empty userAccount}">
        <a href="/login">登入</a>
    </c:when>
    <c:otherwise>
        <a href="/logout">登出</a>
    </c:otherwise>
</c:choose>

for 迴圈

for 回圈的基本語法如下

<c:forEach var="i" begin="1" end="5">
${i}<br>
</c:forEach>

底下是九九乘法表,請在 Web Page 新增 99.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>九九乘法表</title>
    </head>
    <body>
        <table border="1" cellpadding="0" cellspacing="0" align="center">
            <c:forEach var="i" begin="1" end="9">
                <tr>
                    <c:forEach var="j" begin="1" end="9">
                        <td width="50">
                            ${i*j}
                        </td>
                    </c:forEach>
                </tr>
            </c:forEach>
        </table>
    </body>
</html>

todo

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *