核心组标签是最常用的JSTL标签,包括:
| Tag | Description |
|---|---|
| <c:out > | 和<%= ... >标签一样功能, 但对于表达式。 |
| <c:set > | 设置表达式求值的结果在一个‘范围‘ |
| <c:remove> | 删除一个范围的变量(从一个特定的范围内,如果已指定)。 |
| <c:catch> | Catches any Throwable that occurs in its body and optionally exposes it. |
| <c:if> | Simple conditional tag which evalutes its body if the supplied condition is true. |
| <c:choose> | Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise> |
| <c:when> | Subtag of <choose> that includes its body if its condition evalutes to ‘true‘. |
| <c:otherwise > | Subtag of <choose> that follows <when> tags and runs only if all of the prior conditions evaluated to ‘false‘. |
| <c:import> | Retrieves an absolute or relative URL and exposes its contents to either the page, a String in ‘var‘, or a Reader in ‘varReader‘. |
| <c:forEach > | The basic iteration tag, accepting many different collection types and supporting subsetting and other functionality . |
| <c:forTokens> | Iterates over tokens, separated by the supplied delimeters. |
| <c:param> | Adds a parameter to a containing ‘import‘ tag‘s URL. |
| <c:redirect > | Redirects to a new URL. |
| <c:url> |
Creates a URL with optional query parameters |
<c:out>显示表达式的结果,类似于<%=表达式%>
语法:
<c:out value="value" [escapeXml="{true|false}"] [default ="defaultValue"] />
| 属性 | 描述 | 必需 | 默认值 |
|---|---|---|---|
| value | 输出的信息 | Yes | None |
| default | 反馈输出的信息 | No | body |
| escapeXml | True,如果标签转义特殊XML字符 | No | true |
eg:
<%@ page language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Tag Example - www.yiibai.com</title>
</head>
<body>
<c:out value="${‘this a string type ‘}"/>
</body>
</html>
//结果输出为:
this a string type
<c:set> 用于保存数据的语法,是<jsp:setProperty>行为标签的孪生兄弟,计算表达式的值,然后使用计算结果来设置javabean对象或java.util.Map对象的值。
语法:
<c:set value="value" var="varName" [scope="{ page|request|session|application }"] /> //将value的值储存至范围为scope的varName变量之中
| 属性 | 描述 | 必需 | 默认值 |
|---|---|---|---|
| value | 需要保存的信息 | No | body |
| target | 为一JavaBean或java.util.Map对象 | No | None |
| property | 指定target对象的属性 | No | None |
| var | 欲存入的变量名称 | No | None |
| scope | var变量的JSP范围 | No | Page |
eg:
<%@ page language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Tag Example - www.yiibai.com</title>
</head>
<body>
<hr>设置userName的属性为hellking,然后输出这个属性值:
<c:set value="hellking" var="userName" />
<c:out value="${userName}"/>
<hr>设置password的属性,属性值在body中,然后输出这个属性值:
<c:set var="password">
1234
</c:set>
<hr>设置javaBean的属性,然后输出这些属性值:
<c:set value="hk2" target="${user}" property="userName" />
<c:set target="${user}" property="password">
1234
</c:set>
userName=<c:out value="${user.userName}" />
password=<c:out value="${user.password}" />
<hr>设置不同的属性,并且制定它们的范围:
<c:set value="1000" var="maxUser" scope="application" />
<c:set value="100" var="maxIdelTime" scope="session" />
<c:set value="next.jsp" var="nextPage" scope="page" />
</body>
</html>
<c:remove> 用于删除数据
语法:
<c:remove var="varName" [scope="{ page|request|session|application }"] />
| 属性 | 描述 | 必需 | 默认值 |
|---|---|---|---|
| var | 删除的变量名称 | Yes | None |
| scope | 要删除变量的范围 | No | All scopes |
eg:
<%@ page language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Tag Example - www.yiibai.com</title>
</head>
<body>
<c:set var="salary" scope="session" value="${2000*2}" />
删除前:<c:out value="${salary}" /><br>
<c:remove var="salary" />
删除后:<c:out value="${salary}" default="null"/>
</body>
</html>
//结果输出为:
删除前:4000
删除后:null
<c:catch> 主要用来处理产生错误的异常状况,并且将错误信息储存起来
语法:
| 属性 | 描述 | 必需 | 默认值 |
|---|---|---|---|
| var | 变量的名称保存在java.lang。如果抛出的Throwable在body元素内。 | No | None |
<%@ page language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Tag Example - www.yiibai.com</title>
</head>
<body>
<c:catch var="catchException">
<% int x=5/0; %>
</c:catch>
<c:if test="${catchException!=null}">
<c:out value="${catchException}" /><br>
<c:out value="${catchException.message}" />
</c:if>
</body>
</html>
//结果输出为:
java.lang.ArithmeticException: / by zero
/ by zero
<c:if> 相当于if判断
语法:
<c:if test="testCondition" var="varName" [scope="{page|request|session|application}"]/>
| 属性 | 描述 | 必需 | 默认值 |
|---|---|---|---|
| test | 条件计算 | Yes | None |
| var | 变量名称的存储条件的结果 | No | None |
| scope | 变量的范围的存储条件的结果 | No | page |
eg:
<%@ page language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Tag Example - www.yiibai.com</title>
</head>
<body>
<c:set var="salary" scope="session" value="${2000*2 }" />
<c:if test=${salary>2000 }>
<p>My salary is:<c:out value="${salary }" /></p>
</c:if>
</body>
</html>
//结果输出为:
My salary is 4000
<c:choose> 相当于switch语句
语法:
<c:choose>
<c:when> //case
</c:when>
...
<c:otherwise> //default
<c:otherwise>
</c:choose>
| 属性 | 描述 | 必需 | 默认值 |
|---|---|---|---|
| test | 条件计算 | Yes | None |
eg:
<%@ page language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Tag Example - www.yiibai.com</title>
</head>
<body>
<c:set var="salary" scope="session" value="${2000*2 }" />
<c:choose>
<c:when test="${salary<=0 }">
Salary is very low to survive.
</c:when>
<c:when test="${salary>1000 }">
Salary is very good.
</c:when>
<c:otherwise>
No comment sir...
</c:otherwise>
</c:choose>
</body>
</html>
//结果输出为:
Salary is very good.
<c:import> 提供了所有<jsp:include>行为标签所具有的功能,同时也允许包含绝对URL
语法:
<c:import [var="varName"] url="url" [context=""] [charEncoding=""] [scope="{page|request|session|application}"] [varReader="varReader"]>
| 属性 | 描述 | 必需 | 默认值 |
|---|---|---|---|
| url | 需要检索和引入的页面的URL | 是 | 无 |
| context | / 紧接着一个本地网络应用程序的名称 | 否 | 当前应用程序 |
| charEncoding | 所引入的数据的字符编码集 | 否 | ISO-8859-1 |
| var | 用于存储所引入的文本的变量 | 否 | Print to page |
| scope | var属性的作用域 | 否 | Page |
| varReader | 可选的用于提供java.io.Reader对象的变量 | 否 | 无 |
eg:
<%@ page language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Tag Example - www.yiibai.com</title>
</head>
<body>
<c:import var="data" url="http://www.w3cschool.cc" charEncoding="UTF-8"/>
<c:out value="${data}" />
</body>
</html>
<c:forEach> 循环控制
语法:
| 属性 | 描述 | 必需 | 默认值 |
|---|---|---|---|
| items | 要进行迭代的集合 | No | None |
| begin | 如果指定了begin,那么迭代就从items[begin]开始进行迭代;如果没有指定items,那么就从0开始迭代。它的类型为整数 | No | 0 |
| end | 如果指定了end,那么就在items[end]结束迭代;如果没有指定items,那么就在items最后结束迭代。它的类 型也为整数 | No | Last element |
| step | 迭代的步长 | No | 1 |
| var | 迭代参数的名称。在迭代体中可以使用的变量的名称,用来表示每一个迭代变量 | No | None |
| varStatus | 迭代变量的名称,用来表示迭代的状态,可以访问到迭代自身的信息 | No | None |
注意:
eg:
<%@ page language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Tag Example - www.yiibai.com</title>
</head>
<body>
<%
String atts[]=new String[5];
atts[0]="hello";
atts[1]="this";
atts[2]="is";
atts[3]="a";
atts[4]="pen";
request.setAttribute("atts",atts);
%>
<c:forEach items="${atts}" var="item">
<c:out value="${item}" />
</c:forEach>
<c:forEach items="${atts}" var="item" begin="0" end="4" step="2">
<c:out value="${item}" />
</c:forEach>
</body>
</html>
//结果输出为:
hello this is a pen
hello is pen
<c:forTokens> 根据指定的分隔符来分隔内容并迭代输出 ,用法类似于<c:forEach>
属性多一个:
| 属性 | 描述 | 必需 | 默认值 |
|---|---|---|---|
| items | 要进行迭代的集合 | No | None |
| begin | 如果指定了begin,那么迭代就从items[begin]开始进行迭代;如果没有指定items,那么就从0开始迭代。它的类型为整数 | No | 0 |
| end | 如果指定了end,那么就在items[end]结束迭代;如果没有指定items,那么就在items最后结束迭代。它的类 型也为整数 | No | Last element |
| step | 迭代的步长 | No | 1 |
| var | 迭代参数的名称。在迭代体中可以使用的变量的名称,用来表示每一个迭代变量 | No | None |
| varStatus | 迭代变量的名称,用来表示迭代的状态,可以访问到迭代自身的信息 | No | None |
| delims | 分隔符 | Yes | None |
eg:
<%@ page language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Tag Example - www.yiibai.com</title>
</head>
<body>
<c:forTokens items="Zara,nuha,roshy" delims="," var="name">
<c:out value="${name}" />
</c:forTokens>
</body>
</html>
//结果输出为:
Zara nuha roshy
<c:param> 用来给包含或重定向的页面传递参数,用于在<c:url>标签中指定参数,而且与URL编码相关
语法:
<c:param name="paramName" [value="value"] />
| 属性 | 描述 | 必需 | 默认值 |
|---|---|---|---|
| name | URL中要设置的参数的名称 | 是 | 无 |
| value | 参数的值 | 否 | Body |
eg:
<%@ page language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Tag Example - www.yiibai.com</title>
</head>
<body>
<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 }" />
</body>
</html>
<c:url> 使用可选的查询参数来创造一个URL,将URL格式化为一个字符串,然后存储在一个变量中,标签在需要的时候自动重写URL.该标签只是用于调用 response.encodeURL()方法的一种可选方法。它提供了合适的URL编码,包括<c:param>中指定的参数。
语法:
<c:url value="url" [context=""] [var=""] [scope="{page|request|session|application}"]>
| 属性 | 描述 | 是否必要 | 默认值 |
|---|---|---|---|
| value | 基础URL | 是 | 无 |
| context | 本地网络应用程序的名称 | 否 | 当前应用程序 |
| var | 代表URL的变量名 | 否 | Print to page |
| scope | var属性的作用域 | 否 | Page |
eg:
<%@ page language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Tag Example - www.yiibai.com</title>
</head>
<body>
<a href="<c:url value="/index.html" />">Test</a>
</body>
</html>
<c:redirect> 重定向至一个新的URL
语法:
<c:redirect url="redirectUrl" [context="projectName"] />
| 属性 | 描述 | 必需 | 默认值 |
|---|---|---|---|
| url | URL重定向用户的浏览器 | Yes | None |
| context | /后面的本地Web应用程序的名称 | No | 当前应用 |
eg:
<%@ page language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Tag Example - www.yiibai.com</title>
</head>
<body>
<c:redirect url="http://www.yiibai.com" />
</body>
</html>
原文:http://www.cnblogs.com/blog-yuesheng521/p/5466069.html