zoukankan      html  css  js  c++  java
  • JSTL

    1.JSTL:

    1). c:out 主要用于对特殊字符进行转换. 真正进行输出时, 建议使用 c:out, 而不是使用 EL
    2). c:set: 可以为域赋属性值。 而对域对象中的 JavaBean 的属性赋值用的并不多.
    3). c:remove: 移除指定域对象的指定属性值(较少使用, 即便移除也是在 Servlet 中完成)

    4). c:if: 在页面上对现实的内容进行过滤, 把结果存储到域对象的属性中. 但不灵活, 会被其他的自定义标签所取代.
    5). c:choose, c:when, c:otherwise: 作用同上, 但麻烦, 不灵活.

    6). c:forEach: 对集合进行遍历的. 常用!
    7). c:forTokens: 处理字符串, 类似于 String 累的 split() 方法(知道即可)

    8). c:import: 导入页面到当前页面的. (了解)
    9). c:redirect: 当前页面进行重定向的. (使用较少)
    10). c:url: 产生一个 URL 的, 可以进行 URL 重写, 变量值编码, 较为常用.

    2. 开发有父标签的标签:

    1). 父标签无法获取子标签的引用, 父标签仅把子标签作为标签体来使用.

    2). 子标签可以通过 getParent() 方法来获取父标签的引用(需继承 SimpleTagSupport 或自实现 SimpleTag 接口的该方法):
    若子标签的确有父标签, JSP 引擎会把代表父标签的引用通过 setParent(JspTag parent) 赋给标签处理器

    3). 注意: 父标签的类型是 JspTag 类型. 该接口是一个空接口, 但是来统一 SimpleTag 和 Tag 的. 实际使用需要进行类型的强制转换.

    4). 在 tld 配置文件中, 无需为父标签有额外的配置. 但, 子标签是是以标签体的形式存在的, 所以父标签的 <body-content></body-content>
    需设置为 scriptless

    5). 实现

    <c:choose>
    <c:when test="${param.age > 24}">大学毕业</c:when>
    <c:when test="${param.age > 20}">高中毕业</c:when>
    <c:otherwise>高中以下...</c:otherwise>
    </c:choose>

    > 开发 3 个标签: choose, when, otherwise
    > 其中 when 标签有一个 boolean 类型的属性: test
    > choose 是 when 和 otherwise 的父标签
    > when 在 otherwise 之前使用

    > 在父标签 choose 中定义一个 "全局" 的 boolean 类型的 flag: 用于判断子标签在满足条件的情况下是否执行.

    若 when 的 test 为 true, 且 when 的父标签的 flag 也为 true, 则执行 when 的标签体(正常输出标签体的内容),
    同时把 flag 设置为 false
    若 when 的 test 为 true, 且 when 的父标签的 flag 为 false, 则不执行标签体.
    若 flag 为 true, otherwise 执行标签体.

    2. 带标签体的自定义标签:

    1). 若一个标签有标签体:

         <atguigu:testJspFragment>abcdefg</atguigu:testJspFragment>

         在自定义标签的标签处理器中使用 JspFragment 对象封装标签体信息.

    2). 若配置了标签含有标签体, 则 JSP 引擎会调用 setJspBody() 方法把 JspFragment 传递给标签处理器类
         在 SimpleTagSupport 中还定义了一个 getJspBody() 方法, 用于返回 JspFragment 对象.

    3). JspFragment 的 invoke(Writer) 方法: 把标签体内容从 Writer 中输出, 若为 null,
          则等同于 invoke(getJspContext().getOut()), 即直接把标签体内容输出到页面上

          有时, 可以 借助于 StringWriter, 可以在标签处理器类中先得到标签体的内容:

        //①. 利用 StringWriter 得到标签体的内容.
                StringWriter sw = new StringWriter();
                bodyContent.invoke(sw);

        //②. 把标签体的内容都变为大写
                String content = sw.toString().toUpperCase();

    4). 在 tld 文件中, 使用 body-content 节点来描述标签体的类型:

         <body-content>: 指定标签体的类型, 大部分情况下, 取值为 scriptless。可能取值有 3 种:
         empty: 没有标签体
         scriptless: 标签体可以包含 el 表达式和 JSP 动作元素,但不能包含 JSP 的脚本元素
         tagdependent: 表示标签体交由标签本身去解析处理。
         若指定 tagdependent,在标签体中的所有代码都会原封不动的交给标签处理器,而不是将执行结果传递给标签处理器

        <body-content>tagdependent</body-content>

    5). 定义一个自定义标签: <atguigu:printUpper time="10">abcdefg</atguigu> 把标签体内容转换为大写, 并输出 time 次到
         浏览器上.

    6). 实现 forEach 标签:

    > 两个属性: items(集合类型, Collection), var(String 类型)
    > doTag:
    * 遍历 items 对应的集合
    * 把正在遍历的对象放入到 pageContext 中, 键: var, 值: 正在遍历的对象.
    * 把标签体的内容直接输出到页面上.

    <c:forEach items="${requestScope.customers }" var="cust2">
    ${pageScope.cust2.id } -- ${cust2.name } <br>
    </c:forEach>

    jstl.jsp

    <%@page import="java.util.HashMap"%>
    <%@page import="java.util.Map"%>
    <%@page import="java.util.ArrayList"%>
    <%@page import="java.util.List"%>
    <%@page import="com.atguigu.javaweb.Customer"%>
    <%@ 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>
    
        <h4>
            c:url 产生一个 url 地址. 可以 Cookie 是否可用来智能进行 URL 重写, 对 GET 请求的参数进行编码
            可以把产生的 URL 存储在域对象的属性中.
            还可以使用 c:param 为 URL 添加参数. c:url 会对参数进行自动的转码. 
            value 中的 / 代表的是当前 WEB 应用的根目录. 
        </h4>
        <c:url value="/test.jsp" var="testurl" scope="page">
            <c:param name="name" value="尚硅谷"></c:param>
        </c:url>
        
        url: ${testurl }
    
        <h4>
            c:redirect 使当前 JSP 页面重定向到指定的页面. 使当前 JSP 转发到指定页面可以使用
            <%--  
            <jsp:forward page="/test.jsp"></jsp:forward>    
            --%>
            / 代表的是当前 WEB 应用的根目录. 
            
            response.sendRedirect("/test.jsp") / 代表 WEB 站点的根目录
        </h4>
        <%-- 
        <c:redirect url="http://www.atguigu.com"></c:redirect>
        <c:redirect url="/test.jsp"></c:redirect>
        --%>
        
        <h4>c:import 可以包含任何页面到当前页面</h4>
        <c:import url="http://www.baidu.com"></c:import>
    
        <h4>c:forTokens: 处理字符串的, 类似于 String 的 split() 方法</h4>
        <c:set value="a,b,c.d.e.f;g;h;j" var="test" scope="request"></c:set>
        <c:forTokens items="${requestScope.test }" delims="." var="s">
            ${s }<br>
        </c:forTokens>
        
        <h4>c:forEach: 可以对数组, Collection, Map 进行遍历, begin(对于集合 begin 从 0 开始算), end, step</h4>
        <c:forEach begin="1" end="10" step="3" var="i">
            ${ i} --
        </c:forEach>
        <br><br>
        
        <% 
            List<Customer> custs = new ArrayList<Customer>();
            custs.add(new Customer(1, "AAA")); //index: 0 
            custs.add(new Customer(2, "BBB")); //1
            custs.add(new Customer(3, "CCC")); 
            custs.add(new Customer(4, "DDD")); //3
            custs.add(new Customer(5, "EEE"));
            custs.add(new Customer(6, "FFF")); //5
            
            request.setAttribute("custs", custs);
        %>
        
        <br><br>
        <!-- 遍历 Collection, 遍历数组同 Collection -->
        <c:forEach items="${requestScope.custs }" var="cust"
            varStatus="status">
            ${status.index}, ${status.count}, ${status.first}, ${status.last}: ${cust.id }: ${cust.name }<br>
        </c:forEach>
        
        <!-- 遍历 Map -->
        <% 
            Map<String, Customer> custMap = new HashMap<String, Customer>();
            custMap.put("a", new Customer(1, "AAA")); //index: 0 
            custMap.put("b", new Customer(2, "BBB")); //index: 0 
            custMap.put("c", new Customer(3, "CCC")); //index: 0 
            custMap.put("d", new Customer(4, "DDD")); //index: 0 
            custMap.put("e", new Customer(5, "EEE")); //index: 0 
            custMap.put("f", new Customer(6, "FFF")); //index: 0 
            
            request.setAttribute("custMap", custMap);
        %>
        
        <br><br>
        <c:forEach items="${requestScope.custMap }" var="cust">
            ${cust.key } - ${cust.value.id } - ${cust.value.name }<br>
        </c:forEach>
        
        <% 
            String [] names = new String[]{"A", "B", "C"};
            request.setAttribute("names", names);
        %>
        <br><br>
        <c:forEach var="name" items="${names }">${name }-</c:forEach>
        
        <br><br>
        <c:forEach items="${pageContext.session.attributeNames }" var="attrName">
            ${attrName }-
        </c:forEach>
        
        <h4>
            c:choose, c:when, c:otherwise: 可以实现 if...else if...else if...else 的效果. 但较为麻烦
            其中: c:choose 以 c:when, c:otherwise 的父标签出现.
            c:when, c:otherwise 不能脱离 c:choose 单独使用.
            c:otherwise 必须在 c:when 之后使用。 
        </h4>
        <c:choose>        
            <c:when test="${param.age > 60 }">
                老年
            </c:when>
            <c:when test="${param.age > 35 }">
                中年
            </c:when>
            <c:when test="${param.age > 18 }">
                青年
            </c:when>
            <c:otherwise>
                未成年. 
            </c:otherwise>
        </c:choose>
    
        <c:set value="20" var="age" scope="request"></c:set>
    
        <h4>c:if: 不能实现 else 操作, 但可以把结果储存起来。 </h4>
        <c:if test="${requestScope.age > 18 }">成年了!</c:if>
        <br><br>
        
        <c:if test="${param.age > 18 }" var="isAdult" scope="request"></c:if>
        isAdult: <c:out value="${requestScope.isAdult }"></c:out>
    
    
        <h4>c:remove: 移除指定域对象的指定属性值</h4>
        <c:set value="1997-09-1" var="date" scope="session"></c:set>
        date: ${sessionScope.date }
        <br><br>
    
        <c:remove var="date" scope="session"/>
        date: --${sessionScope.date }--
        <br><br>
        
        <h4>c:set: 可以为域赋属性值, 其中 value 属性支持 EL 表达式; 还可以为域对象中的 JavaBean 的属性赋值, target, value都支持 EL 表达式</h4>
        
        <c:set var="name" value="ATGUIGU" scope="page"></c:set>
        <%-- 
            pageContext.setAttribute("name", "atguigu");
        --%>
        name: ${pageScope.name }    
        
        <br><br>
        <c:set var="subject" value="${param.subject }" scope="session"></c:set>
        subject: ${sessionScope.subject }
        
        <br><br>
        <% 
            Customer cust = new Customer();
            cust.setId(1001);
            request.setAttribute("cust", cust);
        %>
        ID: ${requestScope.cust.id }
        <c:set target="${requestScope.cust }" property="id" value="${param.id }"></c:set>
        <br>
        ID: ${requestScope.cust.id }
        
        <br><br>
        
        <h4>c:out: 可以对特殊字符进行转换. </h4>
        
        <% 
            request.setAttribute("book", "<<Java>>");
        %>
        book: ${requestScope.book }
        <br><br>
        book: <c:out value="${requestScope.book }" default="booktitle"></c:out>
        
    </body>
    </html>
    All that work will definitely pay off
  • 相关阅读:
    正经学C#_循环[do while,while,for]:[c#入门经典]
    Vs 控件错位 右侧资源管理器文件夹点击也不管用,显示异常
    asp.net core 获取当前请求的url
    在实体对象中访问导航属性里的属性值出现异常“There is already an open DataReader associated with this Command which must be
    用orchard core和asp.net core 3.0 快速搭建博客,解决iis 部署https无法登录后台问题
    System.Data.Entity.Core.EntityCommandExecution The data reader is incompatible with the specified
    初探Java设计模式3:行为型模式(策略,观察者等)
    MySQL教程77-CROSS JOIN 交叉连接
    MySQL教程76-HAVING 过滤分组
    MySQL教程75-使用GROUP BY分组查询
  • 原文地址:https://www.cnblogs.com/afangfang/p/12762834.html
Copyright © 2011-2022 走看看