如果我們已經會建立JSTL的環境,接下來我們深入探討核心標籤庫:
<% taglib prefix="c" uri="java.sun.com/jsp/jstl/core" %>
前綴已經設定為c
Form.jsp
processForm.jsp
<% taglib prefix="c" uri="java.sun.com/jsp/jstl/core" %>
前綴已經設定為c
功能 | 前綴字 | 語法 |
c:catch | 抓取錯誤事件。 | <c:catch var="抓取的變數名稱"> </c:catch> |
c:choose | 等同於if(邏輯條件) | <c:choose> <c:when test="邏輯條件1">值</c:when> <c:when test="邏輯條件2">值</c:when> <c:otherwise>值</c:otherwise> </c:choose> |
c:when | 等同於else if(邏輯條件) | <c:when test="邏輯條件"></c:when> |
c:otherwise | 等同於else | <c:otherwise></c:otherwise> |
c:if | 等同於if | <c:if var="變數" test="邏輯條件1" [scope="page|request|session|application"] |
c:import | 引入某個URL的頁面 | <c:import url="URL位置" [var="變數名稱"] /> |
c:out | 引用jspWrite物件輸出 | 1.<c:out />這是一個title 2.<c:out value="這是一個title" /> |
c:param | 加入參數到此網頁 | <c:param var="變數名稱" value="內容值" [scope="page|request|session|application"] [property="屬性修改"] [target=""] |
c:redirect | 專送到另外網頁位址 | <c:redirect url="URL位址" /> |
c:remove | 移除某一活動變數 | <c:remove var="變數名稱" [scope="page|request|session|application"] |
c:set | 設定單一活動變數或是屬性值 | <c:set></c:set> |
c:url | URL連結,等同於href的內容值 | <c:if var="變數" test="邏輯條件1" [scope="page|request|session|application"] |
c:foreach | 擷取出表單的陣列的資料(如:checkbox) | <c:foreach var="變數值" items="表單欄位值" [begin="數字"] [end="終止值"] [step="累進值"]> </c:foreach> |
EX:
Form.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=BIG5"> <title>Insert title here</title> </head> <body> <form action="processForm.jsp" name="form1" > <input type="radio" name="sex" value="male" />男 <input type="radio" name="sex" value="famale" />女 <br> 運動: <input type="checkbox" name="sport" value="baseball" />baseball <input type="checkbox" name="sport" value="football" />football <input type="checkbox" name="sport" value="soccer" />soccer <input type="checkbox" name="sport" value="swimming" />swimming <input type="checkbox" name="sport" value="badminton" />badminton <br> <input type="submit" name="submit"/> </form> </body> </html> |
processForm.jsp
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ 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>Insert title here</title> </head> <body> <c:choose> <c:when test="${param.sex=='male' }">男</c:when> <c:when test="${param.sex=='famale' }">女</c:when> </c:choose> <c:forEach var="aValue" items="${paramValues['sport']}"> ${aValue} </c:forEach> </body> </html> |
留言
張貼留言