1.JSTL概述
JSTL(JSP Standard Tag Library),JSP標準標簽庫,可以嵌入在jsp頁面中使用標簽的形式完成業務邏輯等功能。jstl出現的目的同el一樣也是要代替jsp頁面中的腳本代碼。JSTL標準標準標簽庫有5個子庫,但隨著發展,目前常使用的是他的核心庫
標簽庫 | 標簽庫的URI | 前綴 |
---|---|---|
Core | http://java.sun.com/jsp/jstl/core | c |
I18N | http://java.sun.com/jsp/jstl/fmt | fmt |
SQL | http://java.sun.com/jsp/jstl/sql | sql |
XML | http://java.sun.com/jsp/jstl/xml | x |
Functions | http://java.sun.com/jsp/jstl/functions | fn |
2.JSTL導入
需要導兩個包:
jstl.jar
standar.jar
使用jsp的taglib指令導入核心標簽庫
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
3.JSTL核心庫的常用標簽
1)<c:if test=””>標簽
其中test是返回boolean的條件
2)<c:forEach>標簽
使用方式有兩種組合形式:
示例:
1)遍歷List的值
2)遍歷List的值
3)遍歷Map<String,String>的值
4)遍歷Map<String,User>的值
5)遍歷Map<User,Map<String,User>>的值
entry.key-----User
entry.value------List<String,User>
//forEach.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="beyond.domain.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><%//模擬List<String> strListList<String> strList = new ArrayList<String>();strList.add("beyond0");strList.add("beyond1");strList.add("beyond2");strList.add("beyond3");request.setAttribute("wsq", strList);//遍歷List<User>的值List<User> userList = new ArrayList<User>();User user1 = new User();//list集合中的第一個元素user1.setId(2);user1.setName("qibao");user1.setPassword("wsq");userList.add(user1);//list集合中的第二個元素User user2 = new User();user2.setId(3);user2.setName("yanyu");user2.setPassword("wsq");userList.add(user2);application.setAttribute("qb", userList);//遍歷Map<String,String>的值Map<String,String> strMap = new HashMap<String,String>();strMap.put("name", "hjj");strMap.put("age", "32");strMap.put("addr", "香港");strMap.put("email", "hjj@qq.com");session.setAttribute("strMap", strMap);//遍歷Map<String,User>的值Map<String,User> userMap = new HashMap<String,User>();userMap.put("user1", user1);userMap.put("user2", user2);session.setAttribute("userMap", userMap); /* //遍歷Map<User,Map<String,User>>的值Map<User,Map<String,User>> mapMap = new HashMap<User,Map<String,User>>();mapMap.put(user1,userMap);mapMap.put(user2,userMap);session.setAttribute("mapMap", mapMap); */%><h1>取出strList的數據</h1><c:forEach items="${wsq}" var="str">${str}<br/></c:forEach><h1>取出userList的數據</h1><c:forEach items="${qb}" var="user" >user的name:${user.name}------------user的password:${user.password} <br/></c:forEach><h1>取出strMap的數據</h1><c:forEach items="${strMap}" var="entry">${entry.key}---------------${entry.value}<br/></c:forEach><h1>取出userMap的數據</h1><c:forEach items="${userMap}" var="entry" >${entry.key}--------------${entry.value.id}--------------${entry.value.name}--------------${entry.value.password}<br/></c:forEach><%-- <h1>取出mapMap的數據</h1><c:forEach items="${mapMap}" var="enter">${entry.key.key}--------------${entry.value.value.id}--------------${entry.value.name}--------------${entry.value.password}<br/></c:forEach> --%></body>
</html>
//userLogin.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ page import= "beyond.domain.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><!-- 模擬用戶已經登陸成功 --><% User user = new User();user.setId(1014);user.setName("wsq");user.setPassword("wsq");session.setAttribute("user", user);/* 把user放到session域當中 */%>
</body>
</html>
//jstl.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><!-- 通過taglib導入jstl的core庫 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><!-- jstl標簽經常會和el表達式(從域中取東西)配合使用 --><%request.setAttribute("count", 1014);%><c:if test="${count==1014}">nice you are right!!</c:if><!-- test代表的是一個返回boolean的表達式條件,要么是true要么是false,如果是true進入if標簽內部,但是,注意這里沒有else標簽!!! --><c:if test="1==1">beyond</c:if><c:if test="1!=1">sq</c:if><!-- 第一種組合方式 --><!-- forEach模擬for(int i=0;i<=5;i++){System.out.println(i);} --><c:forEach begin="0" end="5" var="i">${i}<br/></c:forEach><!-- 也就是從0開始到5結束每次將值賦值給i然后通過el表達式輸出i的值,每輸出一個就換行 --><!-- 第二種組合方式 --><!-- forEach模擬增強for循環 productList----List<Product>for(Product product : productList){}System.out.println(product.getPname());--><!-- items:是一個集合或者數組 var:代表集合中的某一個元素 --><c:forEach items="${productList}" var="pro">${pro.pname}</c:forEach></body>
</html>