a)使用步骤:
i.第一步:导入jar(三个jar包)
ii.第二步:引入jstl的命名空间
b)JSTL的常用的标签
(1)if标签
(2).choose标签:类似于Java代码中的if else
(3).forEach标签:类似于java中for循环
i.遍历map集合
ii.遍历list集合
JSTLServlet.java****创建map集合、创建list集合、将flag+map+list分别通过request.setAttribute放到request的域中去,然后通过request.getRequestDispatcher()forward()然后访问demo3.jsp
package cn.java.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.sun.org.apache.xalan.internal.xsltc.compiler.sym;
import cn.java.entity.User;
/**
* Servlet implementation class SessionServlet
*/
@WebServlet("/JSTLServlet")
public class JSTLServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("flag","1");
//创建map集合
Map<String,Object> hm=new HashMap<String,Object>();
hm.put("username","王二麻子");
hm.put("gender", "1");
hm.put("age","18");
request.setAttribute("hm1", hm);
/*for(Map.Entry<String, Object> entry:hm.entrySet()){
String key=entry.getKey();
Object value=entry.getValue();
System.out.println(key+"="+value);
}*/
//创建一个list集合、
List< Map<String,Object>> lists=new ArrayList< Map<String,Object>>();
lists.add(hm);
lists.add(hm);
lists.add(hm);
lists.add(hm);
request.setAttribute("lists", lists);
request.getRequestDispatcher("/pages/test/demo3.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
doGet(request, response);
}
}
demo3.jsp****JSTL标签是和EL表达式一起使用的。EL中有算数运算、关系运算和逻辑运算,EL表达式是用来获取域中保存的数据。
接下来常用的JSTL标签有<c:if>类似于if标签、<c:choose>(<c:when><c:otherwise> )类似于if-else标签、<c:forEach items="${hm1}" var=“entry”> 类似于for循环对map和list遍历
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" isELIgnored="false"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String context = request.getContextPath();// /dt41_javaweb2
String path = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+context+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=path %>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>jstl标签库</title>
</head>
<body>
<!-- if标签 -->
<c:if test="${2==2}">
<h1>hello jstl</</h1>
</c:if>
<c:if test="${flag!=null}">
<h1>hello world</</h1>
</c:if>
<!-- 类似于if else标签 -->
<c:choose>
<c:when test="${flag!=null}">hello python</c:when>
<c:otherwise>hello mongodb</c:otherwise>
</c:choose>
<!-- forEach对map集合的遍历 -->
<c:forEach items="${hm1}" var="entry">
<h1>${entry.key }:${entry.value}</h1>
</c:forEach>
<!-- 对list集合遍历 -->
<table border="1px" cellspacing="0px" width="600px">
<tr>
<th>用户名</th>
<th>性别</th>
<th>年龄</th>
</tr>
<c:forEach items="${lists}" var="map">
<tr>
<td>${map.username}</td>
<td>
<c:if test="${map.gender==1}">男</c:if>
<c:if test="${map.gender==0}">女</c:if>
</td>
<td>${map.age}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
运行结果如图所示