• JSP(四)----JSTL


    ##  JSTL

    1、概念:JavaServer  Page  Tag  Library  JSP标准标签库

      *  是由Apache组织提供的开源的免费的jsp标签

    2、作用:用于简化可替换JSP页面上的java代码

    3、使用步骤:

      1、导入jstl相关jar包

      2、引入标签库:taglib指令:<%@  taglib  %>

    <!--jstl-->
        <dependency>
          <groupId>jstl</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
        </dependency>
        <dependency>
          <groupId>taglibs</groupId>
          <artifactId>standard</artifactId>
          <version>1.1.2</version>
        </dependency>

      3、使用标签

    4、常用的JSTL标签

      1、if:相当于java代码的if语句

    <%@ page import="java.util.List" %>
    <%@ page import="java.util.ArrayList" %><%--
      Created by IntelliJ IDEA.
      User: 21seu.ftj
      Date: 2020/3/29
      Time: 13:55
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="f" uri="http://java.sun.com/jsp/jstl/core" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <%--
    c:if标签
    1、属性:
        *  test 必须属性,接收boolean表达式
            *  如果表达式为true,则显示if标签体内容;如果false,则不显示标签内容
            *  一般情况下,test属性值会结合el表达式一起使用
    2、注意:f:if标签没有else情况,想要else则再定义一个if的jstl
    
    --%>
    <f:if test="true">
        <h1>我是真...</h1>
    </f:if>
    <br>
    <%
    //判断request域中的一个list集合是否为空,如果不为空则显示遍历集合
        List list = new ArrayList();
        list.add("111");
        request.setAttribute("list",list);
    %>
    <f:if test="${not empty list}">
        遍历集合...
    </f:if>
    </body>
    </html>

      2、choose:相当于java代码的switch语句

    <%--
      Created by IntelliJ IDEA.
      User: 21seu.ftj
      Date: 2020/3/29
      Time: 14:05
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <%--完成数字编号对应星期几的案例--%>
    <%
    request.setAttribute("number",4);
    %>
    <c:choose>
        <c:when test="${number == 1}">星期1</c:when>
        <c:when test="${number == 2}">星期2</c:when>
        <c:when test="${number == 3}">星期3</c:when>
        <c:when test="${number == 4}">星期4</c:when>
        <c:when test="${number == 5}">星期5</c:when>
        <c:when test="${number == 6}">星期6</c:when>
        <c:when test="${number == 7}">星期日</c:when>
        <c:otherwise>数字输入有误</c:otherwise>
    </c:choose>
    </body>
    </html>

      3、foreach:相当于java代码的for语句

    <%@ page import="java.util.ArrayList" %>
    <%@ page import="java.util.List" %><%--
      Created by IntelliJ IDEA.
      User: 21seu.ftj
      Date: 2020/3/29
      Time: 14:12
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <%--
        foreach:java代码的for语句
            1、完成重复的操作
            for(int i = 0; i < 10; i++){}
    
                *  属性:
                    begin:开始值
                    end:结束值
                    var:临时变量
                    step:步长  增长数
                    varStatus:循环状态对象  count:循环的次数  index:容器中的索引,从0开始
    
            2、遍历容器
            List<User> list;
            for(User user : list){}
    
                *  属性:
                    items:容器对象
                    var:容器中元素的临时变量
    --%>
    
    <c:forEach begin="1" end="10" var="i" step="1" varStatus="s">
        ${i}${s.index}${s.count}
    </c:forEach>
    <hr>
    <%
        List list = new ArrayList<>();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        request.setAttribute("list",list);
    %>
    <c:forEach items="${list}" var="li">
        ${li}
    </c:forEach>
    </body>
    </html>
    
    
    <%@ page import="java.util.ArrayList" %>
    <%@ page import="java.util.List" %><%--
      Created by IntelliJ IDEA.
      User: 21seu.ftj
      Date: 2020/3/29
      Time: 14:12
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <%--
        foreach:java代码的for语句
            1、完成重复的操作
            for(int i = 0; i < 10; i++){}
    
                *  属性:
                    begin:开始值
                    end:结束值
                    var:临时变量
                    step:步长  增长数
                    varStatus:循环状态对象  count:循环的次数  index:容器中的索引,从0开始
    
            2、遍历容器
            List<User> list;
            for(User user : list){}
    
                *  属性:
                    items:容器对象
                    var:容器中元素的临时变量
    --%>
    
    <c:forEach begin="1" end="10" var="i" step="1" varStatus="s">
        ${i}${s.index}${s.count}
    </c:forEach>
    <hr>
    <%
        List list = new ArrayList<>();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        request.setAttribute("list",list);
    %>
    <c:forEach items="${list}" var="li">
        ${li}
    </c:forEach>
    </body>
    </html>
















    <%@ page import="com.ftj.domain.User" %>
    <%@ page import="java.util.ArrayList" %>
    <%@ page import="java.util.List" %><%--
      Created by IntelliJ IDEA.
      User: 21seu.ftj
      Date: 2020/3/29
      Time: 14:29
      To change this template use File | Settings | File Templates.
      练习:把User对象的List集合。使用jstl,el展示到jsp页面的表格table中
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <%--把User对象的List集合。使用jstl,el展示到jsp页面的表格table中--%>
    <%
    
        List list = new ArrayList<>();
        list.add(new User(1, "张三", "123", "男"));
        list.add(new User(2, "李四", "321", "女"));
        list.add(new User(3, "王五", "213", "男"));
        request.setAttribute("list", list);
    %>
    <table border="1" width="500" align="center">
        <th>编号</th>
        <th>姓名</th>
        <th>密码</th>
        <th>性别</th>
        <c:forEach items="${list}" var="u">
            <c:if test="${u.id % 2 == 1}">
                <tr style="background-color: darkkhaki">
                    <td>${u.id}</td>
                    <td>${u.username}</td>
                    <td>${u.password}</td>
                    <td>${u.hehe}</td>
                </tr>
            </c:if>
            <c:if test="${u.id % 2 == 0}">
            <tr>
                <td>${u.id}</td>
                <td>${u.username}</td>
                <td>${u.password}</td>
                <td>${u.hehe}</td>
            </tr>
            </c:if>
        </c:forEach>
    </table>
    
    </body>
    </html>
    That which doesn't kill me makes me stronger!
  • 相关阅读:
    Solution 「UVA 1104」Chips Challenge
    Solution 「WF2011」「BZOJ #3963」MachineWorks
    Solution 「洛谷 P4198」楼房重建
    Solution 「HDU 6643」Ridiculous Netizens
    Solution 「多校联训」排水系统
    [非专业翻译] Mapster 配置位置
    [非专业翻译] Mapster 使用特性标签配置映射
    [非专业翻译] Mapster 自定义命名约定
    [非专业翻译] Mapster 配置实例
    [非专业翻译] Mapster 自定义映射
  • 原文地址:https://www.cnblogs.com/21seu-ftj/p/12592151.html
Copyright © 2020-2023  润新知