06.JSTL(基本标签库)
1、JSTL
- 概述:
(JavaServer Page Strandard Tag Linrary):JSP标准标签库
是由Java社区所定制的,提供给JavaWeb开发人的一个通用的自定义标签库
Core:核心标签库
Format:格式化标签
SQL:sql标签库
XML:xml标签库
Function:常用函数标签库
2、使用
1、加入Jar包
jstl-1.2.jar
taglibs-standard-compat-1.2.5.jar
taglibs-standard-impl-1.2.5.jar
taglibs-standard-jstlel-1.2.5.jar
taglibs-standard-spec-1.2.5.jar
…
2、引入自定义标签描述文件
<%@ taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c” %>
3、核心标签库
表达式操作
a)输出:out
向页面输出内容,如果内容中存在特殊符号,将可以自动进行转义操作
value:要输出的内容,可以是EL
*default:当value中的内容为空,则输出该默认值
^escapeXml:关闭转义操作
b)设置:set
将值存入作用域或者JavaBean对象中
var:存入时所使用key
value:存入的值
scope:存入值得作用域,默认值为page
page
request
session
application
c)移除:remove
从作用域中移除某个值
var:被移除的键值对key
scope:移除的键值对所在的作用域
异常:catch
在页面上做异常处理
var:当被该标签圈起来的代码发生异常的时候,会将异常信息存入该属性指定的变量中
d)流程控制:
分支
if:只有if没有else,更没有else-if
test:对应if语句后面括号中的内容,如果该test取值为true则执行if中的内容,否则不执行
var:可以在对应作用域中声明变量,用于存放test后面表达式的值
scope:var指定的变量所存放的作用域
choose:对标if-else if-else
when:如果后面的test中的表达式为true,则执行when中的内容
test:结果为布尔类型的表达式,用于判断,决定when中的内容是否执行
otherwise:对应ifelse中的else,当所有的when都没有执行的时候,则执行该标签中的内容
迭代操作:forEach(迭代、创建循环)
创建循环
var:声明循环变量
begin:初始化循环变量
end:设置循环条件
step:步长
迭代
var:迭代出来的对象的key
items:被去除数据的集合
varStatus:当前迭代的状态
index:当前取出值得索引值
count:循环当前进行的次数
first:如果当前去除的元素为第一个,则该值为true
last:如果当前取出元素为最后一个,则该值为true
如果迭代对象为Map
var:指定的内容为map的键值对对象
key:该键值对的key
value:该键值对的value
forTokens:切割字符串,迭代
items:字符串来源(切割之前)
delims:切割依据
var:切割完成之后的迭代变量
案例:
jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:set value="小明" var="d1" scope="session"></c:set>
<c:set value="小红" var="d2" scope="session"></c:set>
<c:set value="小丽" var="d3" scope="session"></c:set>
<hr/>
<c:remove var="d2" scope="session"/>
<c:out value="${d1 }"></c:out><br/>
<c:out value="${d2 }"></c:out><br/>
<c:out value="${d3 }"></c:out><br/>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:if test="${param.tran<0 }">
<c:out value="开暖气"></c:out>
</c:if>
<c:if test="${param.tran>0 and param.tran<=15 }">
<c:out value="穿秋裤"></c:out>
</c:if>
<c:if test="${param.tran>15 and param.tran<=25 }">
<c:out value="棉袄"></c:out>
</c:if>
<c:if test="${param.tran>25 and param.tran<=35 }">
<c:out value="穿T恤"></c:out>
</c:if>
<c:if test="${param.tran>=40 }">
<c:out value="去冰箱里冻着"></c:out>
</c:if>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:if test="${param.sex=='男' }">
<label>
<input type="radio" name="sex" value="男" checked="checked"/>男
</label>
<label>
<input type="radio" name="sex" value="女"/>女
</label>
</c:if>
<c:if test="${param.sex=='女' }">
<label>
<input type="radio" name="sex" value="男" />男
</label>
<label>
<input type="radio" name="sex" value="女" checked="checked"/>女
</label>
</c:if>
<hr/>
<c:if test=""></c:if>
</body>
</html>
<%@page import="java.util.Set"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.HashMap"%>
<%@page import="com.zuxia.model.StuInfo"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<%
Map<String,StuInfo> map = new HashMap<String,StuInfo>();
map.put("A",new StuInfo(1,"小张","男","1999-09-09","重庆市沙坪坝区",1.75f));
map.put("B",new StuInfo(2,"小王","男","1999-09-09","重庆市沙坪坝区",1.75f));
map.put("C",new StuInfo(3,"小李","男","1999-09-09","重庆市沙坪坝区",1.75f));
map.put("D",new StuInfo(4,"小周","男","1999-09-09","重庆市沙坪坝区",1.75f));
map.put("E",new StuInfo(5,"小找","男","1999-09-09","重庆市沙坪坝区",1.75f));
map.put("F",new StuInfo(6,"小罗","男","1999-09-09","重庆市沙坪坝区",1.75f));
pageContext.setAttribute("map", map);
/* Set<String> stu = map.keySet();
for(String s : stu){
StuInfo sinfo = map.get(s);
out.print(sinfo.getNo());
out.print(sinfo.getName());
out.print(sinfo.getAddress());
out.print(sinfo.getBirthday());
out.print(sinfo.getSex());
out.print(sinfo.getHeight()+"<br/>");
} */
%>
<body>
<table border="1">
<c:forEach items="${map }" var="key">
<tr>
<td>${pageScope.key.value.no }</td>
<td>${pageScope.key.value.name }</td>
<td>${pageScope.key.value.sex }</td>
<td>${pageScope.key.value.birthday }</td>
<td>${pageScope.key.value.address }</td>
<td>${pageScope.key.value.height }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<%
String str = "123,1478,2589,3698587,25874867";
pageContext.setAttribute("str", str);
%>
<body>
<c:forTokens items="${str }" delims="," var="s">
${s }<br/>
</c:forTokens>
<hr/>
<c:forEach var="i" begin="1" end="50" step="5">
你好+${i }<br/>
</c:forEach>
</body>
</html>
<%@page import="java.util.ArrayList"%>
<%@page import="com.zuxia.model.StuInfo"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
List<StuInfo> stus = new ArrayList<StuInfo>();
stus.add(new StuInfo(1,"小张","男","1999-09-09","重庆市沙坪坝区",1.75f));
stus.add(new StuInfo(2,"小王","男","1999-09-09","重庆市沙坪坝区",1.75f));
stus.add(new StuInfo(3,"小李","男","1999-09-09","重庆市沙坪坝区",1.75f));
stus.add(new StuInfo(4,"小周","男","1999-09-09","重庆市沙坪坝区",1.75f));
stus.add(new StuInfo(5,"小找","男","1999-09-09","重庆市沙坪坝区",1.75f));
stus.add(new StuInfo(6,"小罗","男","1999-09-09","重庆市沙坪坝区",1.75f));
pageContext.setAttribute("stus", stus);
%>
<table border="1" align="center" width=500 height=250>
<caption><h3>学生信息表</h3></caption>
<c:forEach var="stu" items="${stus }" varStatus="state">
<tr>
<td>${stu.no }</td>
<td>${stu.name }</td>
<td>${stu.sex }</td>
<td>${stu.birthday }</td>
<td>${stu.address }</td>
<td>${stu.height }</td>
<td>${state.index }</td>
<td>${state.count }</td>
<td>${state.first }</td>
<td>${state.last }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
StuInfo.java
package com.zuxia.model;
public class StuInfo {
private int no;
private String name;
private String sex;
private String birthday;
private String address;
private float height;
public StuInfo(){
};
public StuInfo(int no, String name, String sex, String birthday, String address, float height) {
super();
this.no = no;
this.name = name;
this.sex = sex;
this.birthday = birthday;
this.address = address;
this.height = height;
}
public String getAddress() {
return address;
}
public String getBirthday() {
return birthday;
}
public float getHeight() {
return height;
}
public String getName() {
return name;
}
public int getNo() {
return no;
}
public String getSex() {
return sex;
}
public void setAddress(String address) {
this.address = address;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public void setHeight(float height) {
this.height = height;
}
public void setName(String name) {
this.name = name;
}
public void setNo(int no) {
this.no = no;
}
public void setSex(String sex) {
this.sex = sex;
}
}
jstl使用案例:
案例1:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>表达式操作</h1>
<h2>输出标签</h2>
<%
pageContext.setAttribute("msg", "早上好!");
pageContext.setAttribute("bookName", "<<小学生行为守则>>");
%>
${msg }
<c:out value="${msg }"></c:out>
<hr/>
${bookName }
<!-- 如果在输出的内容中有特殊符号:><& -->
<c:out value="${bookName }"></c:out>
<hr/>
<c:out value="${age }" default="没有年龄属性存在于作用域中!"></c:out>
<hr>
<c:out value="${age }" default="没有年龄属性存在于作用域中!" escapeXml="false"></c:out>
</body>
</html>
案例2:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- page,request,session,application -->
<c:set var="dt1" value="2020-11-06" scope="session"></c:set>
<c:set var="dt2" value="2020-11-07" scope="session"></c:set>
<c:set var="dt3" value="2020-11-08" scope="session"></c:set>
<c:remove var="dt2" scope="request"/>
${sessionScope.dt1 }<br/>
${sessionScope.dt2 }<br/>
${sessionScope.dt3 }<br/>
</body>
</html>
案例3:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:catch var="e">
<%int num = 10/1;%>
</c:catch>
异常信息:${e }
</body>
</html>
案例4:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:if test="${param.age>=18 }">
<c:out value="可以进入"/>
</c:if>
<c:if test="${param.age<18 }">
<c:out value="未成年人禁止入内"></c:out>
</c:if>
</body>
</html>
案例5:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:if test="${param.age<0 }">
<c:out value="开暖气"/>
</c:if>
<c:if test="${param.age<15 && param.age>=0 }">
<c:out value="羽绒服"></c:out>
</c:if>
<c:if test="${param.age<25 && param.age>=15 }">
<c:out value="棉袄"></c:out>
</c:if>
<c:if test="${param.age<35 && param.age>=25 }">
<c:out value="T恤"></c:out>
</c:if>
<c:if test="${param.age<40 && param.age>=35 }">
<c:out value="吹空调"></c:out>
</c:if>
<c:if test="${param.age>=40 }">
<c:out value="避暑"></c:out>
</c:if>
</body>
</html>
案例6:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
${param.sex }
<hr/>
<c:if test="${param.sex eq '男' }">
<label>
<input type="radio" name="sex" value="男" checked="checked">男
</label>
<label>
<input type="radio" name="sex" value="女">女
</label>
</c:if>
<c:if test="${param.sex eq '女' }">
<label>
<input type="radio" name="sex" value="男">男
</label>
<label>
<input type="radio" name="sex" value="女" checked="checked">女
</label>
</c:if>
<hr/>
<label>
<input type="radio" name="sex1" value="男" ${param.sex eq '男'?'checked':'' }>男
</label>
<label>
<input type="radio" name="sex1" value="女" ${param.sex eq '女'?'checked':'' }>女
</label>
</body>
</html>
案例7:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<hr/>
<c:if test="${param.sex eq '男' }" var="abc" scope="page">
<c:out value="你好"></c:out>
</c:if>
<c:if test="${abc }">
<c:out value="猛男"/>
</c:if>
<c:if test="${not abc }">
<c:out value="你不太好 猛女"></c:out>
</c:if>
</body>
</html>
案例8:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
${param.age }
<hr/>
<c:choose>
<c:when test="${param.age < 13 }">
小学僧
</c:when>
<c:when test="${param.age < 16 }">
初中生
</c:when>
<c:when test="${param.age < 19 }">
高中生
</c:when>
<c:when test="${param.age < 23 }">
打工人
</c:when>
<c:otherwise>
空巢老人
</c:otherwise>
</c:choose>
</body>
</html>
案例9:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="java.util.*,com.zuxia.entity.*"
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
table,td,th{
border:1px solid #CCCCCC;
}
table{
width:100%;
margin:60px auto;
}
</style>
</head>
<body>
<%
List<StuInfo> listSi = new ArrayList<StuInfo>();
listSi.add(new StuInfo(1,"STU0001","小王","女",10,1.1f));
listSi.add(new StuInfo(2,"STU0002","老张","女",10,1.1f));
listSi.add(new StuInfo(3,"STU0003","老李","女",10,1.1f));
listSi.add(new StuInfo(4,"STU0004","老赵","女",10,1.1f));
listSi.add(new StuInfo(5,"STU0005","老罗","女",10,1.1f));
listSi.add(new StuInfo(6,"STU0006","老龚","女",10,1.1f));
pageContext.setAttribute("stus", listSi);
/*
out.print("<table>");
for(StuInfo si : listSi){
out.print("<tr>");
out.print("<td>"+si.getStuId()+"</td>");
out.print("<td>"+si.getStuNo()+"</td>");
out.print("<td>"+si.getStuName()+"</td>");
out.print("<td>"+si.getStuSex()+"</td>");
out.print("<td>"+si.getStuAge()+"</td>");
out.print("<td>"+si.getStuHeight()+"</td>");
out.print("</tr>");
}
out.print("</table>");
*/
%>
<table>
<caption>学生信息表</caption>
<tr>
<th>学生编号</th>
<th>学生学号</th>
<th>学生姓名</th>
<th>学生性别</th>
<th>学生年龄</th>
<th>学生身高</th>
</tr>
<c:forEach var="si" items="${pageScope.stus }" varStatus="sts">
<tr>
<td>${pageScope.si.stuId }</td>
<td>${pageScope.si.stuNo }</td>
<td>${pageScope.si.stuName }</td>
<td>${pageScope.si.stuSex }</td>
<td>${pageScope.si.stuAge }</td>
<td>${pageScope.si.stuHeight }</td>
<td>${sts.index }</td>
<td>${sts.count }</td>
<td>${sts.first }</td>
<td>${sts.last }</td>
</tr>
</c:forEach>
</table>
<hr/>
<c:forEach var="i" begin="0" end="99" step="50">
早上好+${i }<br/>
</c:forEach>
</body>
</html>
案例10:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="java.util.*,com.zuxia.entity.*"
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
table,td,th{
border:1px solid #CCCCCC;
}
table{
width:100%;
margin:60px auto;
}
</style>
</head>
<body>
<%
Map<String,StuInfo> mapStus = new HashMap<String,StuInfo>();
mapStus.put("A",new StuInfo(1,"STU0001","小王","女",10,1.1f));
mapStus.put("B",new StuInfo(2,"STU0002","老张","女",10,1.1f));
mapStus.put("C",new StuInfo(3,"STU0003","老李","女",10,1.1f));
mapStus.put("D",new StuInfo(4,"STU0004","老赵","女",10,1.1f));
mapStus.put("E",new StuInfo(5,"STU0005","老罗","女",10,1.1f));
mapStus.put("F",new StuInfo(6,"STU0006","老龚","女",10,1.1f));
pageContext.setAttribute("stus", mapStus);
%>
<table>
<caption>学生信息表</caption>
<tr>
<th>MapKey</th>
<th>学生编号</th>
<th>学生学号</th>
<th>学生姓名</th>
<th>学生性别</th>
<th>学生年龄</th>
<th>学生身高</th>
</tr>
<c:forEach var="ent" items="${stus }">
<tr>
<td>${pageScope.ent.key }</td>
<td>${pageScope.ent.value.stuId }</td>
<td>${pageScope.ent.value.stuNo }</td>
<td>${pageScope.ent.value.stuName }</td>
<td>${pageScope.ent.value.stuSex }</td>
<td>${pageScope.ent.value.stuAge }</td>
<td>${pageScope.ent.value.stuHeight }</td>
</tr>
</c:forEach>
</table>
<%
/*
Set<String> keys = mapStus.keySet();
for(String key : keys){
StuInfo si = mapStus.get(key);
}
*/
/*
out.print("<table>");
for(StuInfo si : listSi){
out.print("<tr>");
out.print("<td>"+si.getStuId()+"</td>");
out.print("<td>"+si.getStuNo()+"</td>");
out.print("<td>"+si.getStuName()+"</td>");
out.print("<td>"+si.getStuSex()+"</td>");
out.print("<td>"+si.getStuAge()+"</td>");
out.print("<td>"+si.getStuHeight()+"</td>");
out.print("</tr>");
}
out.print("</table>");
*/
%>
</body>
</html>
案例11:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="java.util.*,com.zuxia.entity.*"
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here&