JSTL学习笔记(核心标签)
一、JSTL标签分类:
- 核心标签
- 格式化标签
- SQL标签
- XML标签
- JSTL函数
二、核心标签 引用方式:<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
1.<c:out>可使用“.”符号来访问属性。例如,要访问customer.address.street 只使用标签<c:out value="customer.address.street"/>。<c:out>具有三个属性,value,default(Fallback),escapeXML。
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>JSTL Study</title> </head> <body> this is c:out <br> <c:out value="exciting"/> </body> </html>
2.<c:set>,具有以下属性:value,target,property,var,scope,<c:remove>具有(var,scope)属性
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <html> <head> <base href="<%=basePath%>"> <title>JSTL Study</title> </head> <body> this is c:set <br> <c:set var="example" scope="session" value="${2000*2}"/><br> <c:out value="${example}"/><br> <c:remove var="example"/> <p>After Remove</p> <c:out value="${example}"/> <c:out value="${message.id}"/><br> <c:set target="${message}" property="id" value="123456"/> <c:out value="${message.id}"/> <c:out value="${message.content }" /> <c:out value="${message.description }" /> <c:out value="${message.command }" /> </body> </html>
3.<c:catch>捕获错误,<c:if>具有(test,var,scope)属性,test:条件计算,var:变量名称来存储条件的结果,scope:变量的范围来存储条件的结果
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <html> <head> <base href="<%=basePath%>"> <title>JSTL Study</title> </head> <body> <c:catch var="catchException"> <% int x = 5/0; %> </c:catch> <c:if test = "${catchException != null}"> <p>The exception is : ${catchException} <br /> There is an exception: ${catchException.message}</p> </c:if> </body> </html>
4.<c:choose>就像switch语句。switch有case,<c:choose>有<c:when>。switch有default,<c:choose>有<c:otherwise>
<c:choose> <c:when test="${age <= 18}"> Kid </c:when> <c:when test="${age> 18}"> Not Kid </c:when> <c:otherwise> Illegal </c:otherwise> </c:choose>
5.<c:import>导入功能,具有(url,context,charEncoding,var,scope,varReader)属性
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <html> <head> <base href="<%=basePath%>"> <title>JSTL Study</title> </head> <body> <c:import var="data" url="http://www.baidu.com" /> <c:out value="${data}"/> </body> </html>
6.<c:forEach>遍历对象的集合。<c:forTokens>类似于split
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <html> <head> <base href="<%=basePath%>"> <title>JSTLStudy</title> </head> <body> ForEach and ForTokens<br> <c:forEach items = "${MessageList}" var="mes"> <c:out value="${mes.command}"/> </c:forEach> <c:forTokens items="a,b,c" delims="," var="name"> <c:out value="${name}"/><p> </c:forTokens> </body> </html>
7.<c:param>标签允许适当的URL请求参数并在URL中指定。<c:url>标记格式的URL转换为字符串,并将其存储到一个变量
<c:url value="/index.jsp" var="myURL"> <c:param name="trackingId" value="1234"/> <c:param name="reportType" value="summary"/> </c:url> <c:import url="${myURL}"/>
8.<c:redirect>标签提供自动URL重写将浏览器重定向到另一个URL,它支持上下文相关的URL,并且它支持<c:param>标签。
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <html> <head> <base href="<%=basePath%>"> <title>JSTLStudy</title> </head> <body> <c:redirect url="http://www.baidu.com"/> </body> </html>
感谢http://www.yiibai.com/jsp/jsp_standard_tag_library.html

浙公网安备 33010602011771号