<%@ page import="java.util.Map" %> <%@ page import="java.util.HashMap" %> <%@ page import="java.util.List" %> <%@ page import="com.yuyan.pojo.Student" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%-- Created by IntelliJ IDEA. User: lenovo Date: 2021/8/6 Time: 20:50 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <%-- i.<c:set /> 作用:set 标签可以往域中保存数据 域对象.setAttribute(key,value); scope 属性设置保存到哪个域 page 表示 PageContext 域(默认值) request 表示 Request 域 session 表示 Session 域 application 表示 ServletContext 域 var 属性设置 key 是多少 value 属性设置值 --%> <h3><c:set >(使用很少)作用:set 标签可以往域中保存数据</h3> abc保存之前的值:${sessionScope.abc}<br> <c:set scope="session" var="abc" value="abcValue"/> abc保存之后的值:${sessionScope.abc}<br> <%-- ii.<c:if /> if 标签用来做 if 判断。 test 属性表示判断的条件(使用 EL 表达式输出) --%> <hr> <h3><c:if >:if 标签用来做 if 判断</h3> <c:if test="${true}"> if条件为true </c:if> <c:if test="${false}"> if条件为false </c:if> <%-- iii.<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 标签 --%> <hr> <h3><c:choose> <c:when> <c:otherwise>标签 作用:多路判断。跟 switch ... case</h3> <% request.setAttribute("key",99); %> <c:choose> <c:when test="${requestScope.key>100}"> 关键字key大于100<br> </c:when> <c:when test="${requestScope.key>110}"> 关键字key大于110<br> </c:when> <c:when test="${requestScope.key>105}"> 关键字key大于105<br> </c:when> <c:otherwise> <c:choose> <c:when test="${requestScope.key<100}"> 关键字key小于100<br> </c:when> </c:choose> </c:otherwise> </c:choose> <hr> <h3><c:forEach>作用:遍历输出使用。</h3> <h4>1. 遍历 1 到 10,输</h4> <%--1.遍历 1 到 10,输出 begin 属性设置开始的索引 end 属性设置结束的索引 var 属性表示循环的变量(也是当前正在遍历到的数据) for (int i = 1; i < 10; i++) --%> <c:forEach begin="1" end="10" var="i"> ${i} </c:forEach> <h4>2. 遍历 Object 数</h4> <%-- 2.遍历 Object 数组 for (Object item: arr) items 表示遍历的数据源(遍历的集合) var 表示当前遍历到的数据 --%> <% request.setAttribute("arr",new String[]{"aaa","bbb","ccc"}); %> <c:forEach items="${requestScope.arr}" var="arri"> ${arri} </c:forEach> <h4>3. 遍历 Map 集</h4> <% Map<String,String> map = new HashMap<>(); map.put("key1","value1"); map.put("key2","value2"); map.put("key3","value3"); request.setAttribute("maparr",map); // for ( Map.Entry<String,String> entry : map.entrySet()) {} %> <c:forEach items="${requestScope.maparr}" var="maparri"> ${maparri} 或者 ${maparri.key}-->${maparri.value}<br> </c:forEach> <h4>4. 遍历 List 集合---list 中存放 Student 类,id,name,sex,phone</h4> <% List<Student> list = Student.getStudent(); request.setAttribute("stuList",list); %> <table border="1" cellspacing="0"> <tr> <th>学号</th> <th>姓名</th> <th>性别</th> <th>电话</th> </tr> <c:forEach items="${requestScope.stuList}" var="student"> <%-- ${student}:输出了所有 --%> <tr> <td>${student.id}</td> <td>${student.name}</td> <td>${student.sex}</td> <td>${student.phone}</td> </tr> </c:forEach> </table> </body> </html>
jsp中的JSTL表达式
最新推荐文章于 2021-09-28 19:40:43 发布