JSTL :JSP Standard Tag Library
使用核心标签
首先要在JSP页面上使用taglib指令,三个指令
-
include
-
page
-
taglib语法
-
-
在页面上使用taglib指令
-
在页面中使用c:标签名
prefix:表示标签的前缀,通常写成c(core), uri是一个固定的标识
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
以后标签库中所有的标签语法:
<c:标签名/>
作用
<c:if test="${判断条件}">代码</c:if>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>if标签的使用</title>
</head>
<body>
<%--输入了c:if,按alter+enter idea会自动导入--%>
<c:if test="${param.age < 18}">
未成年人不能进入网吧
</c:if>
<c:if test="${param.age >= 18}">
请办卡
</c:if>
</body>
</html>
作用
如果要使用两个以上的多分支判断就必须使用choose标签,类似于java中switch语句
与when,otherwise标签一起使用
choose标签是一个容器,包含两个子标签
<c:choose>
满足这个条件就执行它下面的代码,没有break,也不会穿透,类似于case
<c:when test="${判断条件}">
代码
</c:when>
<c:when test="${判断条件}">
代码
</c:when>
<c:otherwise> 类似于default
如果上面所有的条件都不满足就执行这个代码段
</c:otherwise>
</c:choose>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>多分支判断的语句</title>
</head>
<body>
<form action="demo09_choose.jsp" method="get">
分数:<input type="text" name="score">
<input type="submit" value="等级">
</form>
<%--如果分数不为空,才进行多分支的判断--%>
<c:if test="${not empty param.score}">
<c:choose>
<c:when test="${param.score >=80 && param.score<=100}">
优秀
</c:when>
<c:when test="${param.score >=70 && param.score<80}">
良好
</c:when>
<c:when test="${param.score >=60 && param.score<70}">
及格
</c:when>
<c:when test="${param.score >=0 && param.score<60}">
不及格
</c:when>
<c:otherwise>
分数有误
</c:otherwise>
</c:choose>
</c:if>
</body>
</html>
-
-
for-i循环
-
forEach遍历一个集合或数组
-
-
以下两个属性的作用是:
-
items:要遍历的集合
-
var:表示每个集合中元素,变量名放在页面域
-
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>forEach标签</title>
</head>
<body>
输出1到100所有的奇数
<hr/>
<%--
forEach标签的属性:
var 变量名,变量的值保存在页面域中
begin 循环的起始值
end 循环的结束值
step 步长,每次跨2步
varStatus 状态对象
--%>
<c:forEach var="i" begin="1" end="100" step="2">
${i}
</c:forEach>
</body>
</html>
| 属性 | 数据类型 | 含义 |
| index | int | 遍历这个元素的索引号,从0开始 |
| count | int | 遍历到了多少个元素 |
| frist | boolean | 判断遍历的单签元素是否是第一个元素,是的话返回true |
| last | boolean | 判断遍历的单签元素是否是最后一个元素,是的话返回true |
package com.itheima.entity; /** * 学生对象 */ public class Student { private int id; //编号 private String name; //名字 private boolean gender; //true男 private double score; //成绩 public Student(int id, String name, boolean gender, double score) { this.id = id; this.name = name; this.gender = gender; this.score = score; } public Student() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isGender() { return gender; } public void setGender(boolean gender) { this.gender = gender; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } }
package com.itheima.servlet; import com.itheima.entity.Student; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.ArrayList; import java.util.List; @WebServlet("/student") public class StudentServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1. 得到所有的学生信息,以后是从数据库中查询 List<Student> studentList = new ArrayList<>(); //按alt选中多行,可以一次直接输入多行代码 studentList.add(new Student(1, "皮卡丘", true, 2)); studentList.add(new Student(2, "小火龙", false, 5)); studentList.add(new Student(3, "杰尼龟", true, 3)); studentList.add(new Student(4, "妙蛙种子", true, 7)); studentList.add(new Student(5, "可达鸭", false, 10)); //2. 将学生信息集合放在请求域中 request.setAttribute("studentList", studentList); //3. 转发到JSP页面上显示 request.getRequestDispatcher("demo11_for.jsp").forward(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>显示学生列表</title>
</head>
<body>
<h2>学生信息列表</h2>
<table border="2" width="600">
<tr>
<th>状态</th>
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>成绩</th>
</tr>
<%--
每个学生是一行信息
forEach的属性:
items:要遍历的信息,提前放在作用域中
var:遍历中每个元素,这里表示每个学生对象,是放在页面域中的
varStatus: 每次遍历元素的状态对象,需要指定对象的变量名字
有四个属性:
index: 这个元素的索引号,从0开始
count:第几个元素,从1开始
first:如果是第1个元素,返回true
last:如果是最后1个元素,返回true
--%>
<c:forEach items="${studentList}" var="student" varStatus="row">
<tr>
<td>${row.index} ${row.count} ${row.first} ${row.last}</td>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.gender?"男":"女"}</td>
<td>${student.score}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
想看更多精彩内容,可以关注我的CSDN

浙公网安备 33010602011771号