JSTL学习笔记

5 阅读2分钟

JSTL简介

标准标签库,为开发者提供一系列标签,使用这些标签可以完成一些逻辑处理,必辱循环遍历集合,让代码更简洁。

示例

JSP代码

<table border="1" cellspacing="0" width="800" align="center">
    <tr>
        <th>商品名称</th><th>商品价格</th><th>库存数量</th>
    </tr>
    <%
        for(Product product:productList){
    %>
            <tr>
                <td><%=product.getName() %></td>
                <td><%=product.getPrice() %></td>
                <td><%=product.getCount() %></td>
            </tr>
    <%
        }
    %>
</table>

JSTL代码

<table border="1" cellspacing="0" width="800" align="center">
    <tr>
        <th>商品名称</th><th>商品价格</th><th>库存数量</th>
    </tr>
    <C:forEach items="${productList}" var="product">
    <tr>
        <td>${product.name}</td>
        <td>${product.price}</td>
        <td>${product.count}</td>
    </tr>
    
    </c:forEach>
</table>

JSTL标签表格

标签名作用
<c:out>用于在JSP中显示数据,就像<%=...>
<c:set>用于保存数据
<c:remove>用于删除数据
<c:catch>用于处理产生错误的异常状况,并且将错误信息储存起来
<c:if>就是if
<c:choose>本身只当作<c:when>和<c:otherwise>的父标签
<c:when><c:choose>的子标签,用来判断条件是否成立
<c:otherwise><c:choose>的子标签,接在<c:when>标签后,当<c:when>标签判断为false时执行
<c:import>检索一个绝对或相对URL,然后将其内容暴露给页面
<c:forEach>基础迭代标签,接受多种集合类型
<c:forTokens>根据指定的分隔符来分割内容并迭代输出
<c:param>用来给半酣或重定向的页面传递参数
<c:redirect>重定向至一个馨德URL
<c:url>使用可选的查询参数来创造一个URL

注意

使用JSTL需要引入

jstl.jar
standard.jar

这两个包到项目--WebContent--WEB-INF--lib中

在JSP页面开始的地方导入JSTL核心标签库代码

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

JSTL常用标签

if标签

<c:if>语法

<c:if  test="codition"> //test用于定义表达式条件
    代码......
</c:if>

示例

<%@ page language="java" contentType="text/html; charset=UTF-8"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>if标签</title>
<%
    Integer age=19;
    request.setAttribute("theAge",age);
%>
</head>

<body>
<c:if test="${theAge>=18 }">
    <h2>您以成年</h2>
</c:if>
<c:if test=${theAge<18 }">
<h2>未成年</h2>
</body>
</html>

forEach标签

<c:forEach> 语法

<c:forEach items="collection" var="varName">
    代码......
</c:forEach>
  • items:被遍历的容器 相当于x
  • var:遍历产生的临时变量 相当于 int i
  • varStatus:遍历状态对象 相当于x的取值 示例
<%@page import="com.wskj.middle.javaWeb.productWeb.Product"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page laguage="java" contentType="text/html; charset=TUF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://hava.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UFT-8">
        <title>forEach 标签</title>
        <% List productList=new ArrayList(); productList.add(new Product("笔记本电脑",5000.00, 100)); productList.add(new Product("手机",2000.00, 200)); productList.add(new Product("鼠标",20.00, 300)); productList.add(new Product("键盘",25.00, 300)); productList.add(new Product("耳机",35.00, 100)); request.setAttribute("productList", productList); %>
    </head>
    <body>
        <h2 align="center">商品列表</h2>
        <table border="1" cellspacing="0" width="800" align="center">
            <tr>
                <th>名称</th><th>价格</th><th>库存</th>
            </tr>
            <c:forEach items="S{productList}" var="product">
                <tr>
                    <td>${product.name}</td>
                    <td>${product.price}</td>
                    <td>${product.count}</td>
                </tr>
            </c:forEach>
        </table>
    </body>
</html>

用法二

语法

<c:forEach items="collection" var="varName" begin="start" end="end" step="stepSize">
    代码....
</c:forEach>
  • begin:开始
  • end:结束
  • setp步长

示例

<%@page import="com.wskj.middle.javaWeb.productWeb.Product"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page laguage="java" contentType="text/html; charset=TUF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://hava.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UFT-8">
        <title>forEach 标签</title>
        <% List productList=new ArrayList(); productList.add(new Product("笔记本电脑",5000.00, 100)); productList.add(new Product("手机",2000.00, 200)); productList.add(new Product("鼠标",20.00, 300)); productList.add(new Product("键盘",25.00, 300)); productList.add(new Product("耳机",35.00, 100)); request.setAttribute("productList", productList); %>
    </head>
    <body>
        <h2 align="center">商品列表</h2>
        <table border="1" cellspacing="0" width="800" align="center">
            <tr>
                <th>名称</th><th>价格</th><th>库存</th>
            </tr>
            <c:forEach items="S{productList}" var="product" begin="0" end="2" step="1">
                <tr>
                    <td>${product.name}</td>
                    <td>${product.price}</td>
                    <td>${product.count}</td>
                </tr>
            </c:forEach>
        </table>
    </body>
</html>