JSTL学习总结

1.什么是JSTL

JSTL(JavaServerPages Standard Tag Library)JSP标准标签库

2.JSTL的作用

使用JSTL实现JSP页面中逻辑处理。如判断、循环等。

3.使用JSTL

(1) 在JSP页面添加taglib指令.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
(2) 使用JSTL标签.
if test="">if>

4.常用标签介绍

核心标签库:
(1) 通用标签 : set ,out ,remove
设置变量  : set var="num" value="${10+5}" scope="page">set>
输出数据 : out value="${num}">out>
移除变更 : var="num"/>
移除后输出 :out value="${num}" default="aaa">out>
(2) 条件标签 : if ,choose
<c:set var="num" value="5">c:set>

<c:if test="${num >3}">
    结果为: true
c:if>

<c:set var="num" value="30">c:set>

<c:choose>
    <c:when test="${num==1}">
        第一名
    c:when>
    <c:when test="${num==2}">
        第二名
    c:when>
    <c:when test="${num==3}">
        第三名
    c:when>
    <c:otherwise>
        没名次
    c:otherwise>      
c:choose>
(3) 迭代标签 : foreach
  • 普通循环

<c:forEach var="i" begin="2" end="10" step="2">
    <hr/>${i}
c:forEach>
  • 迭代器
    • for(类型 变量名 : 数组或集合)
<%
//遍历: list set map和数组
List list = new ArrayList();
list.add("aaa");
list.add("bbb");
list.add("ccc");
list.add("ddd");
list.add("eee");
pageContext.setAttribute("list",list);
%>
<table border="1">
    <tr>
        <th>数据th>
        <th>索引th>
        <th>计数th>
        <th>第一个th>
        <th>最后一个th>
    tr>

    <c:forEach items="${list}" var="num" varStatus="vs">
        <tr class="${vs.index%2==0?'odd':'even'}">
            <td>${num}td>
            <td>${vs.index}td>
            <td>${vs.count}td>
            <td>${vs.first}td>
            <td>${vs.last}td>
        tr>
    c:forEach>
table>

<style type="text/css">
    .odd{
        background-color:#c3f3c3;
    }
    .even{
        background-color:#f3c3f3;
    }
style>

你可能感兴趣的:(jsp,JSTL)