javaweb学习笔记 JSTL标签库篇

a) JSTL标签库的定义

JSTLA标签库 全程是指JSP Standard Tag Library,JSP标准标签库,是一个不断完善的开发源代码的JSP标签库
EL表达式主要是为了替换jsp中的表达式脚本,而标签库则是为了替换代码脚本,这样使得整个jsp页面变得更加整洁
JSTL由五个不同功能的标签库组成

功能范围URI前缀
核心标签库—重点http://java.sun.com/jsp/jstl/corec
格式化http://java.sun.com/jsp/jstl/fmtfmt
函数http://java.sun.com/jsp/jstl/functionsfn
数据库(不常用)http://java.sun.com/jsp/jstl/sqlsql
xml(不常用)http://java.sun.com/jsp/jstl/xmlx

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

 CORE 标签库 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  XML 标签库<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %> 
FMT 标签库 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
  SQL 标签库 <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %> 
  FUNCTIONS 标签库 <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

b)JSFL标签库的使用步骤

1.先导入jstl标签库的jar包
在这里插入图片描述

2.使用taglib指令引入标签库
在这里插入图片描述

c) core核心库使用

1.<c:set/>

作用:可以往域中保存数据
在这里插入图片描述

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: 26523
  Date: 2021/9/16
  Time: 14:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>core</title>
</head>
<body>
<%--<c:set/>
作用:可以往域中保存数据
如何往雨中保存数据?
域对象.getAttribute(key,value)
保存到哪个域? scope属性设置保存到哪个域
 page表示PageContext域(默认值)
  request表示request域 
  session 表示seesion域 
  application表示servletContext域
key是多少? var属性设置key
value是多少? value属性设置value
--%>
保存之前:${requestScope.abc} <br/>
<c:set scope="request" var = "abc" value="abcValue"/>
保存之后:${requestScope.abc}<br/>
</body>
</html>

2.<c:if/>

if标签用来做if判断,
在这里插入图片描述

<hr/>
<%--2.<c:if/>--%>
<%--if标签用来做if判断,
test属性表示判断的条件 (使用ul表达式输出)
--%>
<c:if test="${12 == 12}">
    <h1>12等于12</h1>
</c:if>
<c:if test="${12 != 12}">
    <h1>12不等于12</h1>
</c:if>

3.<c:choose><c:when><c:otherwise>标签

作用:多路判断,跟switch …case …default 非常接近
在这里插入图片描述

<%--<c:choose><c:when><c:otherwise>标签--%>
<%--    作用:多路判断,跟switch ....case ...default 非常接近
choose 标签开始选择判断
when标签每一种判断情况
test表示当前这种判断情况下的值
otherwise标签表示剩下的情况
<c:choose><c:when><c:otherwise>标签 使用时 需要注意的点
1.标签里不能使用html注释 要使用jsp注释
2.when标签的父标签一定是choose
--%>

<%request.setAttribute("height",178);
%>
<c:choose>
    <c:when test="${requestScope.height >190 }">
        <h2>小巨人</h2>
    </c:when>
    <c:when test="${requestScope.height >180 }">
        <h2>很高</h2>
    </c:when>
    <c:when test="${requestScope.height >170 }">
        <h2>还可以</h2>
    </c:when>
    <c:otherwise>
        <h2>剩下小于170的情况</h2>
    </c:otherwise>
</c:choose>

4.<c:forEach/>

4.1 输出1到10的遍历

在这里插入图片描述

作用:输出遍历使用

<table>
    <c:forEach begin="1" end="10" var="i">
        <tr>
            <td>第${i}</td>
        </tr>
    </c:forEach>
</table>

4.2 遍历object数组

<%--遍历object数组
for(Object : arr)
items属性表示遍历的数据源(遍历的集合)
var 表示当前遍历的数据
--%>
<% request.setAttribute("arr",new String[]{"123456789","111111","dsgdzbfbv"});%>
    <c:forEach items="${requestScope.arr}" var="item" >
      ${item}<br/>
    </c:forEach>

在这里插入图片描述

4.3遍历map集合

在这里插入图片描述

<hr>
<%
Map<String,Object> map = new HashMap<String,Object>();
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");
for(Map.Entry<String,Object> entry : map.entrySet()){

}
request.setAttribute("map",map);
%>
<c:forEach items="${requestScope.map}" var = "entry">
   ${entry.key}<br/>
    ${entry.value}<br/>
</c:forEach>

4.4.遍历 List 集合—list 中存放 Student 类,有属性:编号,用户名,密码,年龄, 电话信息

在这里插入图片描述

foreach.jsp

<%--遍历 List 集合---list 中存放 Student 类,有属性:编号,用户名,密码,年龄, 电话信息--%>
<%
    List<student> list = new ArrayList<student>();
    for (int i = 1; i <10 ; i++) {
        list.add(new student(i,"username"+i ,"pass"+i,18+i,"phone" +i));
    }
    request.setAttribute("studentlist",list);
%>
<table border="1">
    <tr>
        <th>编号</th>
        <th>用户名</th>
        <th>密码</th>
        <th>年龄</th>
        <th>电话</th>
        <th>操作</th>
    </tr>
    <c:forEach items="${requestScope.studentlist}" var ="student">
        <tr>
            <td>${student.id}</td>
            <td>${student.username}</td>
            <td>${student.password}</td>
            <td>${student.age}</td>
            <td>${student.phone}</td>
            <td> 删除 修改 </td>
        </tr>
    </c:forEach>

student.java

package com.atguigu.pojo;

public class student {
//    遍历 List 集合---list 中存放 Student 类,有属性:编号,用户名,密码,年龄, 电话信息


private Integer id;
private String username;
private String password;
private Integer age;
private String phone;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public student() {
    }

    public student(Integer id, String username, String password, Integer age, String phone) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.age = age;
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "student{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                ", phone='" + phone + '\'' +
                '}';
    }
}

4.5 foreach标签的所有属性使用介绍

items表示遍历的集合
var表示遍历到的数据
begin表示遍历的开始索引值
end表示结束的索引值
step属性表示遍历的步长值 i++的步长值为1
varStatus属性表示当前遍历到的数据的状态
它实现接口LoopTagStatus的方法:
Object getCurrent() 表示获取当前遍历到的数据
int getIndex() 表示获取遍历的索引
int GetCount() 表示遍历的个数
boolean isFirst() 表示当前遍历的数据是否为第一条
Boolean isLast() 表示当前遍历的数据是否为最后一条
Integer getBegin() 获取begin属性
integer getEnd() 获取end属性
integer getStep() 获取step属性

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值