zoukankan      html  css  js  c++  java
  • 传智播客JavaWeb day06-jstl

    一、jsp标签(sun公司提供的)

    二、EL表达式

    三、jstl (javaserver pages standard tag library)

    1.为什么要有jstl

      jsp标签太弱,el表达式功能不完整(比如不能遍历集合),所有就有了jstl。目的就是配合jsp+el取代在jsp中嵌入Java代码的方式,提高程序的可读性、维护性、方便性

    2. jstl实质、原理

      jstl的实现过程就相当于el表达式中调用Java方法的实现过程。首先定义类包含静态方法,定义一个tdl文件对函数进行描述,然后在页面上用tablib指令引用文件,然后就可以通过前缀加冒号调用函数了

    3.jstl五个类型的库(核心标签库、国际化标签库、数据库标签库、xml标签库、jstl函数,其中SQL标签库跟xml标签库不常用所以不学)

      3.1 core标签库

        3.1.1 c:out

          3.1.1.1 输出常量         

          3.1.1.2 输出变量

          3.1.1.3 输出默认值(有值就输出值没有值就输出默认值)

          3.1.1.4 html转义输出  ps:escapeXml属性能控制是否按转义输出默认值为true

    jsp代码如下:

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'cout.jsp' starting page</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
      </head>
      
      <body>
       <h1>输出常量</h1><hr>
       <c:out value="你好我这是输出常量"></c:out>
        <h1>输出变量</h1><hr>
        <%String name = "无双";
        pageContext.setAttribute("name",name);
         %>
         <c:out value="${name }"></c:out>
         <h1>输出默认值</h1><hr>
         <c:out value="${name}" default="奥巴马(这个是默认值)"></c:out>
         <c:out value="${name1 }" default="奥巴马(这个是默认值)"></c:out>
         <h1>输出html转义</h1><hr>
         <c:out value="<a href='#'>html转义内容</>" escapeXml="true"></c:out>
      </body>
    </html>
    View Code

    页面效果:

        3.1.2 c:set  ps:var属性有变量则找到变量没有则声明一个变量;value属性设置变量值;scope:指定域;

          3.1.2.1 设置或修改域中属性的值

          3.1.2.2 设置或修改域中map的属性值

          3.1.2.3 设置或修改域中javabean的属性值

    jsp代码:

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'cset.jsp' starting page</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
      </head>
      
      <body>
      <h1>设置域中属性的值</h1>
        <c:set var="name" value="力宏" scope="page"></c:set> <br>
        ${name }
        <c:set var="name" value="王力宏" scope="page"></c:set> <br>
        ${name }
        <h1>设置域中map的属性值</h1><hr>
        <%Map map = new HashMap();
        pageContext.setAttribute("map", map);
         %>
         <c:set target="${map }" property="cellphone" value="10086"></c:set>
         ${map.cellphone}(设置map值)<br>
          <c:set target="${map }" property="cellphone" value="10087"></c:set>
           ${map.cellphone}(修改map值)
           <h1>设置域中javabean的属性值(用法同map)</h1><hr>
      </body>
    </html>
    View Code

    效果图:

        3.1.3 c:remove 移除域中属性的值   <c:remove var="name" scope="page"/>指定变量名称然后指定域

        3.1.4 c:catch 捕获异常(把异常抓起来,捕获了页面上就不会有异常了,用户体验就友好了)    

    <c:catch var="e">
           <%int i= 1/0; %>
           </c:catch>
           ${e.message}
    View Code

    效果:

        3.1.5 c:if 执行判断条件,有一个很重要的属性test接收一个bool值

        3.1.6 c:choose、c:when、c:otherwise

            c:chooose要c:when和c:otherwise配合使用

        3.1.7 c:foreach  ps:items属性指定要遍历的集合

          3.1.7.1 遍历数组 string[]

          3.1.7.2 遍历集合 list

          3.1.7.3 遍历map HashMap、linkedHashMap(按顺序输出)

          3.1.7.4 循环执行指定的内容若干次

        3.1.8 c:forTokens  切割字符串

        3.1.9 c:import  包含文件

        3.1.10 c:redirect、c:param  重定向

        3.1.11 c:url  替代代码进行url重写

    代码:cforeach、cfortokens

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'cforeach.jsp' starting page</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
      </head>
      <%String[] cities = new String[]{"北京","上海","广州"};
      pageContext.setAttribute("city",cities);
        %>
      <body>
      <h1>遍历数组</h1><br>
        <c:forEach items="${city }" var="c"> ${c}<br></c:forEach><br>
        <h1>遍历集合</h1><br>
        <%
        List list = new ArrayList();
        list.add("中国");
        list.add("美国");
        list.add("俄罗斯");
        pageContext.setAttribute("list", list);
         %>
         <c:forEach items="${list}" var="c">${c}<br></c:forEach>
         <h1>遍历map</h1><br>
         <%
         Map map = new LinkedHashMap();
         map.put("name","力宏");
         map.put("age", 19);
         map.put("wife", "呵呵");
         pageContext.setAttribute("map", map);
          %>
          <c:forEach items="${map }" var="entry">
          ${entry.key }:${entry.value }<br>
          </c:forEach>
          <h1>循环执行指定的内容若干次</h1><br>
          <c:forEach begin="1" end="20" step="1" var="i" varStatus="stat">
          <c:if test="${stat.index%2 == 0 }">
          <font color="red"> ${i }</font> </c:if>
          <c:if test="${stat.index%2!= 0 }">
          <font color="blue">${i}</font>
          </c:if>
          </c:forEach>
          <h1>c:forTokens切割字符串</h1><br>
          <c:forTokens items="www.aqioo.com" delims="." var="c">
          ${c }<br>
          </c:forTokens>
      </body>
    </html>
    View Code

    效果图:

    代码:cif

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'cif.jsp' starting page</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
      </head>
      <%int day = 4;
      pageContext.setAttribute("day",day);
       %>
      <body>
        <c:if test="${ 2>1}">就是这样的</c:if> <br>
        <c:choose>
        <c:when test="${day==3 }">星期三</c:when>
        <c:otherwise>
        不是星期三
        </c:otherwise>
        </c:choose>
      </body>
    </html>
    View Code

    代码:credirect

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'credirect.jsp' starting page</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
      </head>
      
      <body>
        <h1>credirect练习</h1> <br>
        <c:redirect url="/jstl/cforeach.jsp" context="${pageContext.request.contextPath }">
        <c:param name="aa" value="nihao"></c:param>
        </c:redirect>
      </body>
    </html>
    View Code

    效果:直接跳转到cforeach.jsp中去了

    代码:curl 当浏览器端禁用cookie后,右键查看源代码就能看到url后跟一个JessesionId

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'curl.jsp' starting page</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
      </head>
      
      <% 
      request.getSession();
      String url =  response.encodeUrl(request.getContextPath()+"/jstl/cforeach.jsp");
        pageContext.setAttribute("url", url);
       %>
      <body>
        <h1>url重写练习</h1> <br>
        <a href="${url }">这个是用Java代码实现的url重写</a>
        <c:url context="${pageContext.request.contextPath }" value="/jstl/cforeach.jsp" scope="page" var="url1"></c:url>
        <a href="${url1 }">这个是用c:url实现的url重写
        </a>
      </body>
    </html>
    View Code
  • 相关阅读:
    Additional Color Tables
    How to make a non-symmetric color pallet around zero
    【cl】cmd相关命令
    【cl】oracle之Sequence
    常用命令之ps
    卸载oracle
    oracle—无法启动
    oracle 导入数据
    proc文件系统漫谈
    GStreamer插件分类
  • 原文地址:https://www.cnblogs.com/lihongchen/p/4346366.html
Copyright © 2011-2022 走看看