JSTL、JSTL核心标签库——流程处理标签

JSTL环境

  JSTL是另一个标准规范,并非在JSP的规范中,所以必须另外下载JSTL实现。
  要使用JSTL标签库,必须在JSP网页上使用taglib指示元素定义前置名称与uri参考。例如,引入JSTL1.1核心标签库:

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

JSTL分类

  JSTL提供的标签库分为五个大类
  1、核心标签库:提供条件判断、属性访问、URL处理及错误处理等标签。
  2、I18N兼容格式标签库:提供数字、日期等的格式化功能,以及区域(Locale)、信息、编码处理等国际化功能的标签。
  3、SQL标签库:提供基本的数据库查询、更新、设置数据源(DataSource)等功能的标签,这会在第9章说明JDBC时再介绍。
  4、XML标签库:提供XML解析、流程控制、转换等功能的标签。
  5、函数标签库:提供常用字串处理的自定义EL函数标签库。


核心标签库

  1、<c:if test="">标签 特别要注意,是test属性,而不是text属性。

<%@page contentType="text/html" pageEncoding="UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
</head>
<body>
<c:if test="${param.name == 'zs' and param.passwd == '123'}">
    欢迎 ${param.name} 登陆
</c:if>
</body>
</html>
index.jsp

测试:127.0.0.1/index.jsp?name=zs&passwd=123

响应:欢迎 zs 登陆

  2、<c:if>标签没有与之相对应的<c:else>标签,要使用<c:choose>、<c:when>及<c:otherwise>才可以。
  <c:choose>中可以有多个<c:when>标签,会从上往下进行测试。如果有个<c:when>标签的test运算结果为true就输出内容,之后的<c:when>就不会做测试。如果所有<c:when>测试都不成立,则会输出<c:otherwise>的内容。

<%@page contentType="text/html" pageEncoding="UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
</head>
<body>
    <c:choose>
        <c:when test="${param.index == 1}">
            欢迎 ${param.name} 超级用户登陆
        </c:when>
        <c:when test="${param.index == 2}">
            欢迎 ${param.name} 管理员登陆
        </c:when>
        <c:when test="${param.index == 3}">
            欢迎 ${param.name} VIP登陆
        </c:when>
        <c:otherwise>
            欢迎 ${param.name} 游客登陆
        </c:otherwise>
    </c:choose>
</body>
</html>
index.jsp

测试:http://127.0.0.1/index.jsp?name=Mike&index=1
响应:欢迎 Mike 超级用户登陆

  3、<c:forEach>标签
  <c:forEach>标签的items属性可以是数组、Collection、Iterator、Enumeration、Map与字符串。

<%@page contentType="text/html" pageEncoding="UTF-8" import="java.util.List, java.util.ArrayList" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    List<String> cities = new ArrayList<String>();
    cities.add("东京");
    cities.add("北京");
    cities.add("纽约");
    cities.add("伦敦");
    session.setAttribute("cities", cities);
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
</head>
<body>
    <table>
    <c:forEach items="${cities}" var="city">
        <tr>
            <td>${city}</td>
        </tr>
    </c:forEach>
    </table>
</body>
</html>
index.jsp

测试:http://127.0.0.1/index.jsp
响应:

东京
北京
纽约
伦敦

  如果items指定的是Map,则设置给var的对象会是Map.Entry,这个对象有getKey()与getValue()方法,可以让你取得键与值。例如:

<%@page contentType="text/html" pageEncoding="UTF-8" import="java.util.Map, java.util.HashMap" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    Map<String, String> cities = new HashMap<String, String>();
    cities.put("日本", "东京");
    cities.put("中国", "北京");
    cities.put("美国", "纽约");
    cities.put("英国", "伦敦");
    session.setAttribute("cities", cities);
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
</head>
<body>
    <table>
    <c:forEach items="${cities}" var="city">
        <tr>
            <td>${city.key}</td>
            <td>${city.value}</td>
        </tr>
    </c:forEach>
    </table>
</body>
</html>
index.jsp

  如果items指定的是字符串,则必须是个以逗号区隔的值,<c:forEach>会自动以逗号来切割字符串,例如:

<%@page contentType="text/html" pageEncoding="UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    session.setAttribute("cities", "东京, 北京, 纽约, 伦敦");
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
</head>
<body>
    <table>
    <c:forEach items="${cities}" var="city">
        <tr>
            <td>${city}</td>
        </tr>
    </c:forEach>
    </table>
</body>
</html>
index.jsp

  如果希望自行指定切割依据,则可以使用<c:forTokens>,例如:

<%@page contentType="text/html" pageEncoding="UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    session.setAttribute("cities", "东京|北京|纽约|伦敦");
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
</head>
<body>
    <table>
    <c:forTokens items="${cities}" var="city" delims="|" >
        <tr>
            <td>${city}</td>
        </tr>
    </c:forTokens>
    </table>
</body>
</html>
index.jsp
原文地址:https://www.cnblogs.com/Mike_Chang/p/10088022.html