jsp's JSTL tag library
- What is jstl
JSTL (JSP Standard Tag Library), the JSP standard tag library, can be embedded in the JSP page using the form of tags to complete business logic and other functions. - What's the point of jstl?
The purpose of jstl is to replace the script code in the jsp page the same as el. - The JSTL standard standard tag library has 5 sub-libraries, and the core library is often used at present
JSTL expression-environment preparation
-
Import the jar package:
-
Introduce the jstl tag library:
<%--引入jstl--%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-8LZ8nNC1-1600141877880)(C:\Users\Carlos\AppData\Roaming\Typora\typora-user-images\ image-20200915111554829.png)]
JSTL expression-if tag
-
There are many core tags in jstl, and the only commonly used tags are if and foreach tags.
-
The <c:if> tag
plays a role in the judgment of java code -
Introduction to if tag attributes
web\demo6_jstl_demo.jsp
<%--使用JSTL来简化以上代码--%>
<%
int a = 200;
int b = 500;
request.setAttribute("a", a);
request.setAttribute("b", b);
%>
<%--
test:测试条件成立
var: 用来保存条件的结果,true或者false
scope: 表示将结果存到哪个域中
--%>
<c:if test="${a > b }" var="bl" scope="session">
<h1 style="color: green">a大于b</h1>
<div></div>
</c:if>
<c:if test="${!(a > b) }">
<h1 style="color: red">a小于b</h1>
</c:if>
running result:
JSTL expression-for tag
- forEach tag
plays the role of for loop of java code - forEach tag attribute introduction
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="com.lbl.bean.User" %>
<%@ page import="com.lbl.bean.Birthday" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--
for标签:
1:普通for
for(int i=0; i<5; i++)
begin: 表示索引开始
end :表示索引结束,包含结束值
var :循环变量 i, jsp会自动的将该值存放在pageContext域中
step :每一次循环的增量
2:增强for
--%>
<%
int num = 10;
request.setAttribute("num", num);
%>
<c:forEach begin="0" end="${num}" var="i" step="1">
<h1 style="color: red;">helloword${i}</h1>
</c:forEach>
<%
ArrayList<String> list = new ArrayList<String>();
list.add("baoqiang1");
list.add("baoqiang2");
list.add("baoqiang3");
request.setAttribute("list", list);
%>
<%--
for( String str: list)
items="${list}" 从域中根据list这个键获取集合对象
var="str" 每次循环时,jstl会自动将集合中的元素赋给str
每次循环时,jstl会自动将str的值存入pageContext域
varStatus="vs" 这个参数会记录当前循环的一些状态信息
vs.count 可以获取当前循环的次数
--%>
<c:forEach items="${list}" var="str" varStatus="vs">
<%--${str}--%>
现在是第${vs.count}次循环<br/>
</c:forEach>
<%
ArrayList<User> list2= new ArrayList<User>();
list2.add(new User("liuyan1",33,"female",new Birthday(1991,12,21)));
list2.add(new User("liuyan2",34,"female",new Birthday(1992,12,21)));
list2.add(new User("liuyan3",38,"female",new Birthday(1993,12,21)));
request.setAttribute("list2", list2);
%>
<c:forEach items="${list2}" var="user">
${user.name} ----${user.birthday.year}<br/>
</c:forEach>
</body>
</html>
running result:
Enhanced for loop:
<%
ArrayList<String> list = new ArrayList<String>();
list.add("baoqiang1");
list.add("baoqiang2");
list.add("baoqiang3");
request.setAttribute("list", list);
%>
<%--
for( String str: list)
items="${list}" 从域中根据list这个键获取集合对象
var="str" 每次循环时,jstl会自动将集合中的元素赋给str
每次循环时,jstl会自动将str的值存入pageContext域
varStatus="vs" 这个参数会记录当前循环的一些状态信息
vs.count 可以获取当前循环的次数
--%>
<c:forEach items="${list}" var="str" varStatus="vs">
<%--${str}--%>
现在是第${vs.count}次循环<br/>
${str}次循环<br/>
</c:forEach>
The operation effect is as follows:
##Traversal of instance objects:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="com.lbl.bean.User" %>
<%@ page import="com.lbl.bean.Birthday" %>
<%@ page import="java.util.ArrayList" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
ArrayList<User> list2= new ArrayList<User>();
list2.add(new User("liuyan1",33,"female",new Birthday(1991,12,21)));
list2.add(new User("liuyan2",34,"female",new Birthday(1992,12,21)));
list2.add(new User("liuyan3",38,"female",new Birthday(1993,12,21)));
request.setAttribute("list2", list2);
%>
<c:forEach items="${list2}" var="user">
${user.name} ----${user.birthday.year}<br/>
</c:forEach>
</body>
</html>
The operation effect is as follows: