JSTL(JSP Standard Tag Library),JSP標準標籤庫,能夠嵌入在jsp頁面中使用標籤的形式完成業務邏輯等功能。jstl出現的目的同el同樣也是要提到jsp頁面中的腳本代碼。JSTL標準標準標籤庫有5個子庫,但隨着發展,目前常使用的是他的核心庫html
標籤庫java |
標籤庫的URIapache |
前綴session |
Coreapp |
http://java.sun.com/jsp/jstl/coredom |
cjsp |
從Apache的網站下載JSTL的JAR包。進入「http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/」網址下載 JSTL的安裝包。jakarta-taglibs-standard-1.1.2.zip,而後將下載好的JSTL安裝包 進行解壓,此時,在lib目錄下能夠看到兩個JAR文件,分別爲jstl.jar和standard.jar。網站
其中,jstl.jar文件包含JSTL規範中定義的接口和相關類,standard.jar文件包含用於 實現JSTL的.class文件以及JSTL中5個標籤庫描述符文件(TLD)spa
將兩個jar包導入工程的lib中code
1)<c:if test=" ">標籤,其中test是返回boolean的條件
*引入標籤庫:<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <% 12 request.setAttribute("count",11); 13 %> 14 15 <!-- test表明的返回 --> 16 <c:if test="${count==10}"> 17 xxx 18 </c:if> 19 <c:if test="count!=10"> 20 yyyy 21 </c:if> 22 23 24 </body> 25 </html>
用法:
將當前用戶放到session域,在訪問其餘資源時,能夠從session域中方便取到用戶,看是否是登錄了
(當沒登陸時首頁顯示 登陸註冊,登陸後顯示用戶名和退出)
2)<c:forEach>標籤
使用方式有兩種
1.
2.
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ page import="domain.*"%> 4 <%@ page import="java.util.*"%> 5 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 6 7 <!DOCTYPE html> 8 <html> 9 <head> 10 <meta charset="UTF-8"> 11 <title>Insert title here</title> 12 </head> 13 <body> 14 <% 15 //遍歷List<String> strList 16 List<String> strList = new ArrayList<String>(); 17 strList.add("itcast"); 18 strList.add("itheima"); 19 strList.add("ithe"); 20 request.setAttribute("strList", strList); 21 22 //遍歷List<User>的值 23 List<User> userList = new ArrayList<User>(); 24 User user2 = new User(); 25 user2.setId("2"); 26 user2.setName("lisi"); 27 user2.setPassword("123"); 28 userList.add(user2); 29 User user3 = new User(); 30 user3.setId("3"); 31 user3.setName("wangwu"); 32 user3.setPassword("123"); 33 userList.add(user3); 34 application.setAttribute("userList", userList); 35 36 //遍歷Map<String,String>的值 37 Map<String, String> strMap = new HashMap<String, String>(); 38 strMap.put("name", "lucy"); 39 strMap.put("age", "18"); 40 strMap.put("addr", "西三"); 41 strMap.put("email", "licy@111.com"); 42 session.setAttribute("strMap", strMap); 43 44 //遍歷Map<String,User>的值 45 Map<String, User> userMap = new HashMap<String, User>(); 46 userMap.put("user2", user2); 47 userMap.put("user3", user3); 48 request.setAttribute("userMap", userMap); 49 %> 50 51 <!-- List<String> strList --> 52 <c:forEach items="${strList }" var="str"> 53 ${str } 54 </c:forEach> 55 <br /> 56 <br /> 57 58 <!-- List<User> --> 59 <c:forEach items="${userList }" var="user"> 60 ${user.name} : ${user.password } 61 <br /> 62 </c:forEach> 63 <br /> 64 <br /> 65 66 <!-- Map<String,String> --> 67 <c:forEach items="${strMap }" var="entry"> 68 ${entry.key } : ${entry.value } 69 <br /> 70 </c:forEach> 71 <br /> 72 <br /> 73 74 <!-- Map<String,User> --> 75 <!-- entry.key對應 String;entry.value對應User--> 76 <c:forEach items="${userMap }" var="entry"> 77 ${entry.key }: ${entry.value.name }-- ${entry.value.password} 78 </c:forEach> 79 80 </body> 81 </html>
結果: