5.jsp EL jstl

jsp
  回顾:
    jsp:java server pages(java服务页面)
  组成:
    html+java+jsp标签
  jsp的作用:
    在html代码中嵌套Java代码
  jsp的脚本:
    <%...%> java代码片段,会放在_jspService方法中
    <%=...%> 输出表达式,相当于out.print() 注意:不能以;结尾,会放在_jspService方法中
    <%!...%> 声明成员
  jsp的注释:
    html的注释:
    java的注释:
    jsp的注释:<%--注释内容--%> 在Java源码和html源文件中都不会显示
  jsp的指令:
    格式:
      <%@ 指令 属性='值'%>
    作用:
      1.jsp页面可以执行哪些内容
      2.声明当前jsp页面具有的属性
    jsp指令的分类:
      page指令☆:
        格式:
          <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
        重要属性:
          import:导入包
          contentType:设置响应流的编码,并且通知浏览器用什么编码打开
          pageEncoding:页面的编码格式

          contentType和pageEncoding的区别与联系:
            1.两者中出现一个,代表两个的编码一样
            2.两者都出现,各自用各自的
            3.两者都不出现,使用默认的编码,iso8859-1
        了解的属性:
          language:指定页面要嵌套的语言
          extends:因为jsp本质是一个servlet,声明当前的servlet继承哪个HttpServlet
          buffer:设置当前jsp页面的缓存
          autoFlush:缓存是否自动刷新
          session:是否启用内置对象session,默认的时候是启用的
          errorPage:指定一个错误页面,一旦页面上出现异常,就跳到指定页面上
          isErrorPage:声明当前页面是一个错误页面,在源码中添加一个内置对象,exception
          isELIgnore:是否忽略el表达式,默认不忽略

page指令:

<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" errorPage="600.jsp" isELIgnored="false"%>
<!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>
    <%
        Map map=new HashMap();
        map.put("a","1");
        session.setAttribute("map",map);
        session.setAttribute("username","tom");
        
        //int j=1/0;
    %>

    ${sessionScope.username }
</body>
</html>

      include指令:静态包含
        格式:
          <%@ include file="包含的页面" %>
          静态包含和动态包含的区别:
            静态包含指的是 include指令;动态包含指的是 dispatcher().include()
            静态包含是把包含页面里的内容先拿到自己的页面中,最后统一编译
            动态包含是单独的编译,最后将结果放在同一个页面中。

请求包含:

include.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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> include页面 <hr/> <%@ include file="i1.jsp" %> <hr/> <%@ include file="i2.jsp" %> </body> </html> i1.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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> iiiiii11111111111 </body> </html> i2.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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> iiiiiiiiii222222222 </body> </html>

      taglib指令:导入标签库
        格式:
          <%@taglib uri%>
        属性:
          prefix:前缀。相当于别名
          uri:指定导入哪个标签库,相当于xml的名称空间
            例如:<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      jsp的内置对象:
        内置对象及其类型:
        request    HttpServletRequest
        response  HttpServletResponse
        out      JspWriter
        session    HttpSessioin

        exception    Throwable
        application   ServletContext
        config      ServletConfig
        pageContext  PageContext
        page      this(Servlet)
      四个域对象: 作用范围
        request    一次请求中
        session    一次会话中
        application   全局(整个项目中)
        pageContext   当前页面
        pageContext的作用:
        1.域对象 xxxAttribute()
        2.获取其他的内置对象 getxxx()方法获取其他的内置对象
        3.操作其他的域对象
          setAttribute(String key,Object value,int scope):存
          scope的取值:
            int APPLICATION_SCOPE:从application域中获取数据
            int PAGE_SCOPE:从pageContext域中获取数据
            int REQUEST_SCOPE:从request域中获取数据
            int SESSION_SCOPE:从session域中获取数据
          getAttribute(String key,int scope):取值
          removeAttribute(String key,int scope):移除
        4.便捷的获取域中对象
          Object findAttribute(String name)依次从pageContext,request,session,application中查找,一旦找到即返回,不再往后查找

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
<%
    request.setAttribute("rname","rvalue");
    //request.setAttribute("aname","rvalue");
    session.setAttribute("sname","svalue");
    application.setAttribute("aname","avalue");
    pageContext.setAttribute("pname", "pvalue");
%>

取request域:<%=request.getAttribute("rname") %><br/>
用pageContext取request域:<%=pageContext.getAttribute("rname", pageContext.REQUEST_SCOPE)%>
<hr/>
快捷查找:<br/>
获取aname:
<%=pageContext.findAttribute("aname") %>

</body>
</html>

 



    jsp动作标签:
        jsp:forward 请求转发,等价于 request.getRequestDispatcher("内部路径").forward(req,resp);
          例如:
            <jsp:forward page="/jsp/action/goto.jsp"></jsp:forward>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
    <%
        request.setAttribute("age", 18);
    %>
    
    <jsp:forward page="/jsp/action/goto.jsp"></jsp:forward>
</body>
</html>


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
    参数:<%=request.getParameter("username") %>
    域中数据:<%=request.getAttribute("age") %>
</body>
</html>

       

       jsp:include 动态包含,等价于 request.getRequestDispatcher("内部路径").include(req,resp);
          例如:
            <jsp:include page="/jsp/action/i1.jsp"></jsp:include>
            静态包含是把页面的内容先拿过来,然后统一翻译成java文件
            动态包含是包含其他servlet的结果。
        jsp:param 参数,配合forward和include使用
        例如:
          <jsp:include page="/jsp/action/i1.jsp">
            <jsp:param value="3" name="age"/>
          </jsp:include><br/>
        等价于
          request.getRequestDispatcher("/jsp/action/i1.jsp?age=3").include(req,resp);

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
jsp:include 动态包含
<hr/>
<jsp:include page="/jsp/action/i1.jsp">
    <jsp:param value="3" name="age"/>
</jsp:include><br/>

<jsp:include page="/jsp/action/i2.jsp"></jsp:include>
</body>
</html>


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
iiiiiiiii1111111111111
<%
    int i=3;
%>

age:<%=request.getParameter("age") %>
</body>
</html>


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
iiiiiiiiiiiiiiii2222222222222
<%-- <%=i %> 无法打印出来--%>
</body>
</html>

 

 

EL:
  el介绍:
    EL表达式代替的是java脚本中的<%=...%>
  格式:
    ${el表达式}
  作用:
    1.获取数据☆☆
    2.执行运算☆
    3.调用java对象
    4.自定义el表达式
  获取数据:

    获取域中的简单数据:
      ${pageScope.xxx} 获取的是pageContext域中的数据
      ${requestScope.xxx} 获取的是request域中的数据
      ${sessionScope.xxx} 获取的是session域中的数据
      ${applicationScope.xxx} 获取的是application域中的数据

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
<%
    request.setAttribute("rname","rvalue");
    session.setAttribute("sname","svalue");
    application.setAttribute("aname","avalue");
    pageContext.setAttribute("pname", "pvalue");
%>
pageContext以前的做法:<%=pageContext.getAttribute("pname") %><br/>
el的做法:${pageScope.pname}
<hr/>

request以前的做法:<%=request.getAttribute("rname") %><br/>
el的做法:${requestScope.rname }
<hr/>

session以前的做法:<%=session.getAttribute("sname") %><br/>
el的做法:${sessionScope.sname }
<hr/>

application以前的做法:<%=application.getAttribute("aname") %><br/>
el的做法:${applicationScope.aname }
<hr/>
</body>
</html>

    

    获取复杂的数据:
      获取数组:
        ${requestScope.arr[2]}
      获取list:
        ${requestScope.list[1]}
      获取map:
        ${requestScope.map.username}

<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
<%
    //往request中放一个数组
    String [] arr={"aa","bb","cc"};
    request.setAttribute("arr",arr);
%>
获取数组:<br/>
el的做法:${requestScope.arr[2] }<br/>
以前的做法:<%=((String[])request.getAttribute("arr"))[2] %>
<hr/>

<%
    //往request中放list
    List<String> list=new ArrayList();
    list.add("aaa");
    list.add("bbb");
    list.add("ccc");
    request.setAttribute("list", list);
%>
获取集合:<br/>
el的做法:${requestScope.list[1] }<br/>
以前的做法:<%=((ArrayList)request.getAttribute("list")).get(1) %>
<hr/>

<%
    //往request中放map
    Map<String,String> map=new HashMap();
    map.put("username","tom");
    map.put("age","18");
    request.setAttribute("map", map);
%>
获取map:</br>
el的做法:${requestScope.map.username}<br/>
以前的做法:<%=((Map<String,String>)request.getAttribute("map")).get("username") %>
</body>
</html>

      

      javabean对象导航:
        javabean:
          1.类必须是公共的,且是具体的,不是抽象的
          2.属性必须提供get或set方法
          3.必须提供一个无参构造
        ${requestScope.user.username
      注意:
         如果直接使用${属性名称|对象名称},默认依次从pageScope,requestScope,sessionScope,applicationScope挨个查找,
          若找到,则返回
          若没有找到,返回一个""

<%@page import="domain.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
    <%
        //往request中放入user
        User user=new User("tom",12,"fger");
        request.setAttribute("user", user);
    %>
    获取对象<br/>
    以前做法:<%=((User)request.getAttribute("user")).getUsername() %></br>
    el做法:${requestScope.user.username }
</body>
</html>

    

    执行运算:
      注意:
        + 只进行加法运算。若有字符串形式的数字,可以转成数字然后进行相加
        empty运算符可以判断对象是否为空以及判断容器的长度是否为0
        el支持三元运算符

<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="domain.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
1+2: ${1+2 }
<%
    int i=3;
    String j="4";
    String q="111";
    request.setAttribute("i", i);
    request.setAttribute("j", j);
    request.setAttribute("q", q);
    
    User user=new User("tom",12,"qwe");
    request.setAttribute("user", user);
    User user1=null;
    request.setAttribute("user1", user1);
    
    List list=new ArrayList();
    list.add(1);
    request.setAttribute("list", list);
%>
<hr/>
i+j:${i+j }
<hr/>

i:${i}
<hr/>
i+k:${i+k } 
<hr/>
j+q:${j+q }
<hr/>
user:${empty user }
<hr/>
user1:${empty user1 }
<hr/>
list:${empty list }
<hr/>
三元:${i==3?"==":"!=" }
</body>
</html>

   

   el的内置对象(11个):
      pageScope
      requestScope
      sessionScope
      applicationScope

      param
      paramValues是

      header
      headerValues

      initParam
      cookie

      pageContext

    注意:
      除了pageContext返回的不是一个map,其他获取的都是map集合。
    关于参数的内置对象:
      param:
        格式:
          ${param}
            获取的格式是 map<String,String>,获取单值的参数
      paramValues
        格式:
          ${paramValues.username}
            获取的格式是 map<String,String[]>,相当于request.getParameterMap
            username:${paramValues.username[0]} 获取具体的值
    关于请求头的内置对象:
      header:获取的格式是 map<String,String>,获取单值的参数
      headerValues:获取的格式是 map<St获取的是 map<String,cookie>ring,String[]>
    关于初始化参数的内置对象:
      initParam:获取的格式是 map<String,String>,获取单值的参数
        获取的是web.xml下context-param标签下的内容(全局的初始化参数)
      <context-param>
        <param-name>driverclass</param-name>
        <param-value>com.mysql.jdbc.Driver</param-value>
      </context-param>
    关于cookie的内置对象:
      cookie:获取的是 map<String,cookie>
      相当于
        Cookie username=new Cookie("username","tom");
        map.put("username",username)
      例如:
        cookie:${cookie }<br/>
        name:${cookie.JSESSIONID.name }</br>
        value:${cookie.JSESSIONID.value }</br>
    关于pageContext:
      最常用的操作为:pageContext.request.contextPath

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
<!-- 
<a href="/javaee_day12/el/demo5.jsp?username=tom&password=123&hobby=eat&hobby=drink">关于参数的内置对象</a><br/>
    获取的格式是 map<String,String>
    例如:username tom
    map.put("username","tom">
    只能获取单值的信息,多的忽略
 -->
param:${param }<br/>

<!-- 
    获取的格式是 map<String,String[]>
    相当于request.getParameterMap
 -->
paramValues:${paramValues }<br/>
username:${paramValues.username[0] }
<hr/>
<hr/>

<!-- 
    获取的是一个map<String,String>
    获取单值的头信息,多的忽略,只要一个
 -->
header:${header }
<hr/>

headerValues:${headerValues }<br/>
referer:${headerValues.connection[0] }

<hr/>
<hr/>
initparam:${initParam }
<hr/>
<hr/>
<!-- 
    获取的是 map<String,cookie>
    
    相当于
        Cookie username=new Cookie("username","tom");
        map.put("username",username)
 -->
cookie:${cookie }<br/>
name:${cookie.JSESSIONID.name }</br>
value:${cookie.JSESSIONID.value }</br>

<hr/>
<hr/>
项目名:${pageContext.request.contextPath }
</body>
</html>

 

   el函数库的使用:
    主要使用的是Jstl提供的函数库
    必须导入标签库
    通过<%@taglib %> 导入两个包,jstl.jar standard.jar

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!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>
${fn:toUpperCase("hello") }
</body>
</html>

 

 

JSTL
  jstl的介绍:
    jsp standard tag library ,jsp标准标签库
  jstl的使用步骤:
    1.导入jstl-1.2.jar和standard.jar
    2.在jsp上导入标签库:
  格式:
    <%@taglib prefix="" uri="" %>
    jstl的分类:
      1.core 核心库
      2.fmt 格式化
      3.sql库
      4.xml xml库
  了解的jstl标签库:
    c:if
    c:set
    c:foreach
    c:choose c:when c:otherwise

    c:out
    c:remove
    c:catch
    c:forTokens
    c:import
    c:url
    c:redirect
    c:param
    c:set
  基本用法:对基本属性的操作
    格式:
      <c:set var="属性名称" value="属性值" [scope="page(默认)|request|session|application"]></c:set>
        高级用法:对对象的操作
          格式:
            <c:set target="域中对象" property="对象的属性" value="属性的值"></c:set>

<%@page import="domain.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
  <%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<!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>
set:用来给域对象里的属性和对象赋值用的
<hr/>
<%
    String username="tom";
    request.setAttribute("username", username);
%>
获取:${username }<br/>
<c:set var="username" value="滔滔" scope="request"></c:set>
<hr/>
再次获取:${username }
<hr/>
<c:set var="age" value="18" scope="session"></c:set>
age:${age }
<hr/>
<hr/>
<%
    User user=new User("rose",1,"qwer");
    session.setAttribute("user", user);
%>
<c:set target="${user }" property="username" value="xuduoduo"></c:set>
<!-- 相当于property先getUsername,value相当于setUsername -->
user对象的username属性:${user.username }
</body>
</html>

       
      c:if 用来判断
        格式:
          <c:if test="el表达式" var="给前面结果起名" scope="将前面的名称放到域中">

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/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>
<c:if test="${3>4 }" var="result1">
    3大于4
</c:if>
<c:if test="${3<=4 }" var="result2"  >
    3不大于4
</c:if>
<hr/>
res:${result1 }
</body>
</html>


      c:choose c:when c:otherwise 相当于if(){}else if(){}...else{}

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/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>
    <c:set var="day" value="7"></c:set>
    <c:choose>
        <c:when test="${day==1 }">
            周1
        </c:when>
        <c:when test="${day==2 }">
            周2
        </c:when>
        <c:when test="${day==3 }">
            周3
        </c:when>
        <c:when test="${day==4 }">
            周4
        </c:when>
        <c:when test="${day==5 }">
            周5
        </c:when>
        <c:when test="${day==6 }">
            周6
        </c:when>
        <c:when test="${day==7 }">
            周7
        </c:when>
        <c:otherwise >
            估计你来自火星
        </c:otherwise>
    </c:choose>
</body>
</html>


      c:foreach 相当于for循环

基础用法:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/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>
基础用法:
<br/>
<c:forEach begin="1" end="10" step="1" var="i">
    ${i }<br/>
</c:forEach>
<!-- 相当于for(int i=1;i<=10;i++){i} -->
<hr/>
<c:forEach begin="1" end="20" step="2" varStatus="vs">
    ${j }--${vs.index }--${vs.count }--${vs.first }--${vs.last }--${vs.current }<br/>
</c:forEach>
</body>
</html>

高级用法:
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.Set"%>
<%@page import="java.util.HashSet"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/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>
<%
    String[] arr={"aa","bb","cc"};
    request.setAttribute("arr", arr);
    
    Set set=new HashSet();
    set.add("tom");
    set.add("taotao");
    request.setAttribute("set", set);
    
    Map map=new HashMap();
    map.put("username", "zhangsan");
    map.put("age", 18);
    request.setAttribute("map", map);
%>
<hr/>
遍历数组:
<hr/>
<c:forEach items="${arr }" var="a" varStatus="vs">
    ${a }--${vs.index }<br/>
</c:forEach>
<!-- 相当于for(String a:arr){} -->
<hr/>
遍历set:
<c:forEach items="${set }" var="s">
    ${s }
</c:forEach>
<hr/>
遍历map:
<c:forEach items="${map }" var="m">
    ${m.key}--${m.value}
</c:forEach>
</body>
</html>

 

案例:用jsp+el+ jstl模拟购物车表单

<%@page import="java.util.HashMap"%>
<%@page import="domain.Product"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/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>
<%
    Map<String,Product> cart=new HashMap();
    cart.put("1", new Product("玫瑰花",1,1000));
    cart.put("2", new Product("钻戒",2,200));
    
    request.setAttribute("cart", cart);
%>
<table border="1">
    <tr>
        <th>id</th>
        <th>商品名称</th>
        <th>商品数量</th>
        <th>商品单价</th>
    </tr>
    <c:set var="total" value="0"></c:set>
    <c:forEach items="${cart }" var="p">
        <tr>
            <td>${p.key }</td>
            <td>${p.value.name }</td>
            <td>${p.value.num }</td>
            <td>${p.value.price }</td>
        </tr>
        <c:set var="total" value="${total+p.value.num*p.value.price }"></c:set>
    </c:forEach>
    <tr>
        <td colspan="4" align="rignt" >
            总价:${total }元
        </td>
    </tr>
</table>

</body>
</html>

 

posted @ 2018-02-07 23:52  一日看尽长安花cxjj  阅读(159)  评论(0)    收藏  举报