《JSTL标签库》

《JSTL标签库》,第1张

《JSTL标签库》

标签库的概念

 JSTL标签库,全称jsp标准标签库,标签库主要是为了替代代码脚本,这样使得jsp页面变得更加简洁。

JSTL由五个不同功能的标签库组成

 在jsp标签库中使用taglib指令引入标签库

 JSTL标签库的使用步骤
  1. 先导入jstl标签库的jar包
  2. 使用taglib指令引入标签库
 core核心库使用

作用:set标签可以往域中保存数据

<%@ taglib prefix="a" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: x2773
  Date: 2021/11/18
  Time: 16:28
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


<%--
    scope属性:保存到哪个域(默认page)
    var属性:key是多少
    value属性:value是多少
--%>
    保存之前:${requestScope.abc}

保存之后:${requestScope.abc}

作用:if标签用来做if判断


<%--
    test属性:表示判断的条件(使用EL表达式输出)
--%>
  
    12等于12
  
标签

作用:多路判断,跟swich...case...default 非常像


    
<%--
    choose标签表示开始选择判断
    when标签表示每一种判断情况
    test属性表示当前这种判断情况的值
    注意的点:不能使用html注释,要使用jsp注释
    when标签的父级必须是choose标签,不管你怎么套
--%>
    
         190}">
            小巨人
        
         180}">
            很高
        
         170}">
            还可以
        
        
            小于170的情况
        
    

作用:遍历输出使用

  1. 遍历1到10,输出
    
    <%--
        begin属性:设置开始的索引
        end属性:设置结束的索引
        var属性:表示遍历循环的变量
    --%>
        
            
            
    
                    第${i}列
    
            
            
        
    
    

  2. 遍历Object数组
    
    <%--
        for(Object item:arr){}
        items属性: 表示遍历的数据源
    --%>
       <% request.setAttribute("arr",new String[]{"12214312","132425tert4","2134reefe2"}); %>
        
            ${item}
        
    
  3. 遍历List集合
    <%@ taglib prefix="a" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ page import="java.util.List" %>
    <%@ page import="com.atguigu.pojo.Student" %>
    <%@ page import="java.util.ArrayList" %><%--
      Created by IntelliJ IDEA.
      User: x2773
      Date: 2021/11/19
      Time: 11:18
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
    
        Title
        
            table{
                width: 500px;
                border: 1px solid red;
                border-collapse: collapse;
            }
            tr,td{
                border: 1px solid red;
            }
        
    
    
        <%
            List list = new ArrayList<>();
            for(int i = 0;i<10;i++){
                list.add(new Student(i,"student"+i,"password"+i,18+i,"phone"+i));
            }
            request.setAttribute("stus",list);
        %>
        
            
                编号
                用户名
                密码
                年龄
                电话
                 *** 作
            
            
                
                    ${stu.id}
                    ${stu.name}
                    ${stu.password}
                    ${stu.age}
                    ${stu.phone}
                    删除
                
            
        
    
    
    

  4. 遍历Map集合
    
    <%--
        for(Object item:arr){}
        items属性: 表示遍历的数据源
        var属性:表示遍历的数据源的接收变量
    --%>
        <%
            Map map = new HashMap();
            map.put("key1","value1");
            map.put("key2","value2");
            map.put("key3","value3");
    //        for(Map.Entry entry:map.entrySet()){
    //
    //        }
            request.setAttribute("map",map);
        %>
        
            ${entry.key} = ${entry.value}
        
    

forEach标签所有属性组合使用介绍
<%@ taglib prefix="a" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.util.List" %>
<%@ page import="com.atguigu.pojo.Student" %>
<%@ page import="java.util.ArrayList" %><%--
  Created by IntelliJ IDEA.
  User: x2773
  Date: 2021/11/19
  Time: 11:18
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title
    
        table{
            width: 500px;
            border: 1px solid red;
            border-collapse: collapse;
        }
        tr,td{
            border: 1px solid red;
        }
    


    <%
        List list = new ArrayList<>();
        for(int i = 0;i<10;i++){
            list.add(new Student(i,"student"+i,"password"+i,18+i,"phone"+i));
        }
        request.setAttribute("stus",list);
    %>
    
        
            编号
            用户名
            密码
            年龄
            电话
             *** 作
        
<%--
    items属性:表示遍历的集合
    var属性:表示遍历到的数据
    begin属性:表示遍历的开始索引值
    end属性:表示遍历的结束索引值
    step属性:表示遍历的步长值
    for(int i = 0;i<10;i+=2){}//i+=2步长值为2
    vaStatus属性:表示当前遍历到的数据的状态
--%>
        
            
                ${stu.id}
                ${stu.name}
                ${stu.password}
                ${stu.age}
                ${stu.phone}
                ${status.current}
            
        
    


 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5564382.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存