<!-- JSTL表达式依赖--> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl-api</artifactId> <version>1.2</version> </dependency> <!-- standard标签库依赖--> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency>
${ }
jspTag01.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <%--可以在请求转发中携带参数--%> <%request.setCharacterEncoding("utf-8");%> <%response.setCharacterEncoding("utf-8");%> <jsp:forward page="jspTag02.jsp"> <jsp:param name="name" value="源浩"/> <jsp:param name="age" value="20"/> </jsp:forward> </body> </html>
jspTag02.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>welcomt to jspTag02</h1> 姓名:<%=request.getParameter("name")%> 年龄:<%=request.getParameter("age")%> </body> </html>
测试访问
JSTL(Java server pages standarded tag library,即JSP标准标签库)是由JCP(Java community Proces)所制定的标准规范,它主要提供给Java Web开发人员一个标准通用的标签库,并由Apache的Jakarta小组来维护。开发人员可以利用这些标签取代JSP页面上的Java代码,从而提高程序的可读性,降低程序的维护难度。
**JSTL标签的使用是为了弥补HTML标签的不足;它定义了很多自定义标签,标签的功能与java代码一样
在使用对应标签之前,要写上引用该标签的语法
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %> <%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
coreif.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Title</title> </head> <body> <h2>if测试</h2> <hr> <form action="coreif.jsp" method="get"> <input type="text" name="username" value="${param.username}"> <input type="submit" value="login"> </form> <c:if test="${param.username=='admin'}" var="isAdmin"> <c:out value="欢迎您,管理员!"></c:out> </c:if> <c:out value="${isAdmin}"></c:out> </body> </html>
测试访问:
coreForEach.jsp
<%@ page import="java.util.ArrayList" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Title</title> </head> <body> <% ArrayList<String> people = new ArrayList<>(); people.add("张三"); people.add("李四"); people.add("王五"); people.add("赵六"); people.add("钱七"); request.setAttribute("peopleList",people); %> <%-- var:每次遍历出来的变量 items:要遍历的对象 begin:开始遍历的位置 end:结束遍历的位置 step:步长 --%> <c:forEach var="peoples" items="${peopleList}"> <c:out value="${peoples}"></c:out> <br> </c:forEach> <hr> <c:forEach var="peoples" begin="0" step="2" items="${peopleList}"> <c:out value="${peoples}"></c:out> <br> </c:forEach> </body> </html>
测试访问:
coreWhen.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Title</title> </head> <body> <c:set var="score" value="90"></c:set> <c:choose> <c:when test="${score>=90}"> 您的成绩为优秀 </c:when> <c:when test="${score>=80}"> 您的成绩为中等 </c:when> <c:when test="${score>=60}"> 您的成绩为合格 </c:when> <c:when test="${score<60}"> 您的成绩为不合格 </c:when> </c:choose> </body> </html>
测试访问: