博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
5.jsp EL jstl
阅读量:4704 次
发布时间:2019-06-10

本文共 15863 字,大约阅读时间需要 52 分钟。

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"%>
Insert title here <% Map map=new HashMap(); map.put("a","1"); session.setAttribute("map",map); session.setAttribute("username","tom"); //int j=1/0; %> ${sessionScope.username }

      include指令:静态包含

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

请求包含:

include.jsp <%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
Insert title hereinclude页面

<%@ include file="i1.jsp" %>

<%@ include file="i2.jsp" %>i1.jsp<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
Insert title hereiiiiii11111111111i2.jsp<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
Insert title hereiiiiiiiiii222222222

      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"%>
Insert title here<% request.setAttribute("rname","rvalue"); //request.setAttribute("aname","rvalue"); session.setAttribute("sname","svalue"); application.setAttribute("aname","avalue"); pageContext.setAttribute("pname", "pvalue");%>取request域:<%=request.getAttribute("rname") %>
用pageContext取request域:<%=pageContext.getAttribute("rname", pageContext.REQUEST_SCOPE)%>

快捷查找:
获取aname:<%=pageContext.findAttribute("aname") %>

 

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

       

       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"%>
Insert title herejsp:include 动态包含

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
Insert title hereiiiiiiiii1111111111111<% int i=3;%>age:<%=request.getParameter("age") %><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
Insert title hereiiiiiiiiiiiiiiii2222222222222<%-- <%=i %> 无法打印出来--%>

 

 

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"%>
Insert title here<% request.setAttribute("rname","rvalue"); session.setAttribute("sname","svalue"); application.setAttribute("aname","avalue"); pageContext.setAttribute("pname", "pvalue");%>pageContext以前的做法:<%=pageContext.getAttribute("pname") %>
el的做法:${pageScope.pname}

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

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

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

    

    获取复杂的数据:

      获取数组:
        ${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"%>
Insert title here<% //往request中放一个数组 String [] arr={"aa","bb","cc"}; request.setAttribute("arr",arr);%>获取数组:
el的做法:${requestScope.arr[2] }
以前的做法:<%=((String[])request.getAttribute("arr"))[2] %>

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

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

      

      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"%>
Insert title here <% //往request中放入user User user=new User("tom",12,"fger"); request.setAttribute("user", user); %> 获取对象
以前做法:<%=((User)request.getAttribute("user")).getUsername() %>
el做法:${requestScope.user.username }

    

    执行运算:

      注意:
        + 只进行加法运算。若有字符串形式的数字,可以转成数字然后进行相加
        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"%>
Insert title here1+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);%>

i+j:${i+j }

i:${i}

i+k:${i+k }

j+q:${j+q }

user:${empty user }

user1:${empty user1 }

list:${empty list }

三元:${i==3?"==":"!=" }

   

   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"%>
Insert title here
param:${param }
paramValues:${paramValues }
username:${paramValues.username[0] }


header:${header }

headerValues:${headerValues }
referer:${headerValues.connection[0] }


initparam:${initParam }


cookie:${cookie }
name:${cookie.JSESSIONID.name }
value:${cookie.JSESSIONID.value }


项目名:${pageContext.request.contextPath }

 

   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" %>
Insert title here${fn:toUpperCase("hello") }

 

 

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" %>
Insert title hereset:用来给域对象里的属性和对象赋值用的

<% String username="tom"; request.setAttribute("username", username);%>获取:${username }

再次获取:${username }

age:${age }


<% User user=new User("rose",1,"qwer"); session.setAttribute("user", user);%>
user对象的username属性:${user.username }

       

      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" %>
Insert title here
3大于4
3不大于4

res:${result1 }

      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" %>
Insert title here
周1
周2
周3
周4
周5
周6
周7
估计你来自火星

      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" %>
Insert title here基础用法:
${i }

${j }--${vs.index }--${vs.count }--${vs.first }--${vs.last }--${vs.current }
高级用法:<%@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" %>
Insert title here<% 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);%>

遍历数组:

${a }--${vs.index }

遍历set:
${s }

遍历map:
${m.key}--${m.value}

 

案例:用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" %>
Insert title here<% Map
cart=new HashMap(); cart.put("1", new Product("玫瑰花",1,1000)); cart.put("2", new Product("钻戒",2,200)); request.setAttribute("cart", cart);%>
id 商品名称 商品数量 商品单价
${p.key } ${p.value.name } ${p.value.num } ${p.value.price }
总价:${total }元

 

转载于:https://www.cnblogs.com/syj1993/p/8429019.html

你可能感兴趣的文章
Centos 6.5下的OPENJDK卸载和SUN的JDK安装、环境变量配置
查看>>
poj 1979 Red and Black(dfs)
查看>>
【.Net基础03】HttpWebRequest模拟浏览器登陆
查看>>
zTree async 动态参数处理
查看>>
Oracle学习之常见错误整理
查看>>
数据库插入数据乱码问题
查看>>
altium annotate 选项设置 complete existing packages
查看>>
【模式识别与机器学习】——SVM举例
查看>>
【转】IT名企面试:微软笔试题(1)
查看>>
IO流入门-第十章-DataInputStream_DataOutputStream
查看>>
DRF的分页
查看>>
Mysql 模糊匹配(字符串str中是否包含子字符串substr)
查看>>
python:open/文件操作
查看>>
流程控制 Day06
查看>>
Linux下安装Tomcat
查看>>
windows live writer 2012 0x80070643
查看>>
tomcat 和MySQL的安装
查看>>
git常用操作
查看>>
京东SSO单点登陆实现分析
查看>>
u-boot启动第一阶段
查看>>