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>