JSTL表达式
JSTL标签库的使用就是为了弥补HTML标签的不足:它自定义许多标签可以供给我们使用,标签的功能和Java代码一样!
去菜鸟教程可以看到JSLT 一些常用方法
核心标签(掌握)
引入了JSTL核心标签库,我们才能使用JSTL标签
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
解决JSTL导包的bug
解决办法:
1.在Tomcat目录下的lib文件夹中加入jstl包
JSTL包下载方法
https://www.cnblogs.com/it-mh/p/12987975.html
- 在maven项目中加入jstl包:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
有时候还需要报错 这时候导入jquery依赖
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.1.1</version>
</dependency>
<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>
举例测试JSTL的几个Demo
c:if
<%--
Created by IntelliJ IDEA.
User: apple
Date: 2021/10/31
Time: 6:14 PM
To change this template use File | Settings | File Templates.
--%>
<%@ 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>
<h3>if测试</h3>
<hr>
<form action="coreif.jsp" method="get">
<input type="text" name="username" value="${param.username}">
<input type="submit" value="登陆">
</form>
<%--判断如果提交的用户名是管理员,则登陆成功--%>
<c:if test="${param.username=='admin'}" var="isadmin" >
<c:out value="管理员欢迎您"></c:out>
</c:if>
<c:out value="${isadmin}" />
</body>
</html>
c:choose c:when
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: apple
Date: 2021/10/31
Time: 8:15 PM
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>
<%--定义一个变量叫score--%>
<c:set var="score" value="85"/>
<c:choose>
<c:when test="${score>=90}">
<h3>你的成绩为优秀</h3>
</c:when>
<c:when test="${score>=80}">
<h3>你的成绩一般</h3>
</c:when>
</c:choose>
</body>
</html>
c:forEach
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.util.ArrayList" %><%--
Created by IntelliJ IDEA.
User: apple
Date: 2021/10/31
Time: 8:20 PM
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>
<%
ArrayList<String> people=new ArrayList<>();
people.add("李四");
people.add("王五");
people.add("赵六");
people.add("田七");
request.setAttribute("list",people);
%>
<c:forEach var="people" items="${list}">
<c:out value="${people}"/><br>
</c:forEach>
</body>
</html>
JSTL 不常用 了解就好~
版权声明:本文为qq_41359998原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。