概括
下载jstl的jar包
官网:https://mvnrepository.com/
网址1:https://search.maven.org/
在pomxml中插入依赖:
<dependency><groupId>taglibs</groupId><artifactId>standard</artifactId><version>1.1.2</version></dependency><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency>
再刷新Maven即可
使用jstl
在jsp页面中填写
使用jsp指令引入jstl标签库
<c:out>标签
HTML编码:空格: 小于:<大于:>
常与el表达式配合使用:
获取http请求消息体中username参数的值(用户输入的值):${param.username}
如:
<%--使用c标签--%>
<c:out value="${param.username}" default="unknown"/>
案例:
代码:
<%--使用c标签1--%>
<c:out value="${param.username}" default="unknown"/><%--使用c标签2--%>
<c:out value="${param.username}" escapeXml="true">
<%-- 输出:<a href="https://editor.csdn.net/md?not_checkout=1&articleId=121206585">CSDN</a>--%><a href="https://editor.csdn.net/md?not_checkout=1&articleId=121206585">CSDN</a>
</c:out>
<c:if>标签
相当于java中的if
案例:
<c:set value="1" var="test" property="test"/>
<%--c:if语句--%>
<c:if test="${test eq 1}">this is your first time!
</c:if>
<c:choose>标签
案例:成绩判断
html代码:
<form action="score.jsp"><label>请输入你的成绩:</label><br/><input type="text" name="score"><br/><input type="submit" value="提交"><input type="reset" value="重置"><br/>
</form>
c:choose代码:
<c:choose><c:when test="${empty param.score}">你还没有输入,请先输入!</c:when><c:when test="${!param.score.matches('-*[0-9]+')}">你输入的成绩不为数字,请重新输入!</c:when><c:otherwise><c:choose><%--成绩越界!--%><c:when test="${(param.score lt 0) or (param.score gt 100)}">你输入的成绩超出范围!</c:when><%--0<=score<=100100 满分90-99 优秀80-89 优良70-79 良好60-69 及格0-59不及格--%><c:when test="${param.score eq 100}">满分!</c:when><c:when test="${(param.score ge 99) and (param.score le 90) }">优秀!</c:when><c:when test="${(param.score ge 80) and (param.score le 89) }">优良!</c:when><c:when test="${(param.score ge 70) and (param.score le 79) }">良好!</c:when><c:when test="${(param.score ge 60) and (param.score le 69) }">及格!</c:when><c:when test="${(param.score ge 0) and (param.score le 59) }">不及格!</c:when><c:otherwise>你输入的不是数字!</c:otherwise></c:choose></c:otherwise>
</c:choose>
总代码:
<%@ page import="java.util.regex.Pattern" %>
<%@ page import="java.util.regex.Matcher" %><%--Created by IntelliJ IDEA.User: DQDate: 2021/11/8Time: 15:08To change this template use File | Settings | File Templates.
--%><%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head><title>成绩</title>
</head>
<body><form action="score.jsp"><label>请输入你的成绩:</label><br/><input type="text" name="score"><br/><input type="submit" value="提交"><input type="reset" value="重置"><br/>
</form><hr/>
你成绩的等级是:<br/><c:choose><c:when test="${empty param.score}">你还没有输入,请先输入!</c:when><c:when test="${!param.score.matches('-*[0-9]+')}">你输入的成绩不为数字,请重新输入!</c:when><c:otherwise><c:choose><%--成绩越界!--%><c:when test="${(param.score lt 0) or (param.score gt 100)}">你输入的成绩超出范围!</c:when><%--0<=score<=100100 满分90-99 优秀80-89 优良70-79 良好60-69 及格0-59不及格--%><c:when test="${param.score eq 100}">满分!</c:when><c:when test="${(param.score ge 99) and (param.score le 90) }">优秀!</c:when><c:when test="${(param.score ge 80) and (param.score le 89) }">优良!</c:when><c:when test="${(param.score ge 70) and (param.score le 79) }">良好!</c:when><c:when test="${(param.score ge 60) and (param.score le 69) }">及格!</c:when><c:when test="${(param.score ge 0) and (param.score le 59) }">不及格!</c:when><c:otherwise>你输入的不是数字!</c:otherwise></c:choose></c:otherwise>
</c:choose></body>
</html>
<c:forEach>标签
案例:
代码:
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--Created by IntelliJ IDEA.User: DQDate: 2021/11/8Time: 15:24To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<%--c:forEach1--%>
<%String[] fruits = {"apple", "orange"};
%>
<c:forEach var="name" items="<%=fruits%>">${name}<br/>
</c:forEach><%--c:forEach2--%>
<%Map map=new HashMap();map.put("name1","dq1");map.put("name2","dq2");
%><c:forEach var="m" items="<%=map%>">${m.key}==${m.value}<br/>
</c:forEach><%--forEach3:普通for方式--%>
<c:forEach var="i" begin="1" end="10">${i}<br/>
</c:forEach>
</body>
</html>
案例2:九九乘法表
代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head><title>九九乘法表改写</title>
</head>
<body>
<table><tr><c:forEach var="i" begin="1" end="9"><c:forEach var="j" begin="1" end="${i}"><td style="border: green solid 2px;padding: 1px">${i}*${j}=${i*j}</td></c:forEach></tr></c:forEach>
</table>
</body>
</html>