JSTL(JavaServer Page Standard Tag Library):JSP标准标签库。它封装了JSP应用的通用核心功能。 1.准备工作 使用JSTL前需要下载所需文件,下载地址及安装教材可参阅:http://www.runoob.com/jsp/jsp-jstl
JSTL(JavaServer Page Standard Tag Library):JSP标准标签库。它封装了JSP应用的通用核心功能。
1.准备工作
使用JSTL前需要下载所需文件,下载地址及安装教材可参阅:http://www.runoob.com/jsp/jsp-jstl.html
测试类 Person.java
public class Person { private String name; private String sex; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
2、c:out标签:类似out.printf(xxx)
<%@ 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> <% request.setAttribute("requestAttribute", "requestValue"); %> <jsp:useBean id = "per" class = "com.myclass.Person" scope = "request" /> <!-- Person per = new Person()--> <jsp:setProperty name = "per" property = "name" value = "zhansan"/> <c:out value = "zhansan"/><br> <!-- out标签直接输出字符串 --> <c:out value = "${requestAttribute}"></c:out><br> <!-- out标签输出属性--> <c:out value = "${per.name}"></c:out><br> <!-- out标签输出对象中属性 --> <!-- 当输出为空时,输出默认值--> <c:out value = "${requestAttri}" default = "该属性不存在!"/><br> <!-- default设置默认值 --> <c:out value = "${requestAttri}">该属性不存在</c:out><br> <!-- 标签体设置默认值 --> <c:out value = "<%=null %>" default = "该属性不存在!"/> </body> </html>
3、c:set标签:主要用于设置属性、对象中属性的值。类似setXXX,setAttribute(var,value);
<head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% request.setAttribute("requestAttri", "requestValue"); %> <jsp:useBean id = "per" class = "com.myclass.Person" scope = "request" /> <!-- Person per = new Person()--> <c:set var = "test" value = "test"/> <!-- 没有指定属性范围,默认是pageScope --> ${pageScope.test} <!-- <c:set var = "属性名称" value = "属性值" scope = "属性作用域" /> --> <c:set var = "sessionName" value = "sessionValue" scope = "session"/> <!-- 在指定范围中设置属性 (value属性设置值) --> <!-- 相当于session.setAttribute("sessionName","sessionValue") --> <c:out value = "${sessionName}" />
<!-- <c:set var = "属性名称" scope = "属性作用域"> 属性值 </c:set> --> <c:set var = "requestName" scope = "request">requestValue</c:set> <!--在指定范围中设置属性 (标签体设置值)--> <!-- 相当于request.setAttribute("requestName","requestValue") --> <c:out value = "${requestName}" />
<!-- <c:set target = "对象名称" property = "属性名" value = "属性值"> --> <c:set target = "${per}" property = "name" value = "zhangsan"/> <!--通过标签 设置对象属性 类似 per.name = "zhangsan"--> <c:out value="${per.name}"></c:out> <c:out value="${per}"></c:out> <!-- 此处的${per} 类似 System.out.print(per)--> </body> </html>
4、c:remove标签:移除属性,类似(xxx.remove(name))
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import = "java.util.*,com.myclass.*" %> <%@ 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 var = "testRemove" value = "value" scope = "session"/> <!-- <c:remove var = "属性名" scoep = "[page|request|session|application]" /> 删除指定作用域中的var属性--> <c:remove var="testRemove" scope="session"/> <!-- 类似 session.removeAttribute("testRemove") --> <c:out value = "${tempRemove }" default="该属性不存在"/> </body> </html>
5、c:if 标签:用于判断,类似if(test == [true | false]){语句}
<%@ 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 var = "age" value = "19" scope = "session"/> <%-- <c:if test = "判断条件" var = "属性名" scope = "设置保存范围" /> --%> <!-- 判断的结果(true|false)会存储在var中 --> <c:if test = "${age >= 18 }" var="isAdult" scope = "session" > <h2> 成年</h2> <!-- 满足条件执行的语句 --> </c:if> <c:if test = "${!isAdult}" > <!-- !isAdult 和isAdult互反所以只会执行一个 --> 未成年 </c:if> 是否成年:${isAdult} </body> </html>
6、c:choose标签:分支选择,类似switch。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import = "java.util.*,com.myclass.*" %> <%@ 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 var = "score" value = "60" scope = "session"/> <c:choose > <c:when test="${score >= 90}"> 优秀</c:when> <!-- test为true则执行对应语句并跳出choose --> <c:when test="${score >= 80}"> 良好</c:when> <c:when test="${score >= 70}"> 一般</c:when> <c:when test="${score >= 60}"> 及格</c:when> <c:otherwise>不及格</c:otherwise> <!-- 所有选项都不满足则执行otherwise中的语句 --> </c:choose> </body> </html>
7、c: forEach标签:主要用于输出可迭代对象和控制迭代次数(for)
实现了Collection接口的对象,都可以使用forEach输出。
<%@ 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> <!-- forEach输出list --> <% List<String> list = new LinkedList<String>(); for(int i = 0; i < 5; i++){ list.add("赵" + i); } request.setAttribute("list", list); %> <c:forEach items = "${list}" var = "li"> <c:out value = "${li}"/><!-- li就代表链表中一个对象(String) --> </c:forEach><br> <!-- forEach输出map --> <% Map<String,Person> map = new HashMap<String,Person>(); for(int i = 0; i < 5; i++){ map.put("" + i,new Person("钱" + i, i % 2 == 0 ? "man" : "woman", i)); } request.setAttribute("map",map); %> <c:forEach items = "${map}" var = "m" ><!-- m代表map中一个对象,可通过m.key获取键,m.value获取值(person对象) --> ${m.key } ${m.value.name} ${m.value.sex} ${m.value.age}<br> </c:forEach><br> <!-- 控制迭代次数 并输出状态--> <table> <td>值</td><td> 是否是第一个</td><td>是否是最后一个</td><td>计数(当前是第几个)</td> <c:forEach var = "i" begin="1" end = "11" step = "2" varStatus="st"> <!-- 步长(step)为2 for(i = 1; i <= 11 ; i += 2 ) --> <tr><!-- 迭代的属性 当前属性是否是第一个 当前属性是否是最后一个 计数是第几个属性,从1开始--> <td>${i }</td><td> ${st.first}</td><td>${st.last }</td><td>${st.count}</td> </tr> </c:forEach> </table> </body> </html>
8、forTokens标签: 将字符串分割成子串然后迭代。类“www.google.com”.split(".")
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import = "java.util.*,com.myclass.*" %> <%@ 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> <!-- 0 1 2 --> <!--类似 l[3] = {"www","google","com"}; for(i = 0; i <=2 ; i+=2) printf(l[i]) --> <c:forTokens items="www.google.com" delims="." var = "l" begin = "0" end = "2" step = "2"> ${l}<br> </c:forTokens> <!-- items为被切割的字符串,delims 为切割的依据 --> </body> </html>
9.c:redirect:重定向将重定向到指定URL。
<%@ page language="java" import = "java.util.*,com.myclass.*" 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:redirect url="http://www.baidu.com"></c:redirect> <!-- 重定向到百度 --> </body> </html>
自定义标签及自定义实现out、if、forEach标签。
参考资料:
https://blog.csdn.net/qq_42246139/article/details/80623607
http://www.runoob.com/jsp/jsp-jstl.html