zoukankan      html  css  js  c++  java
  • JSTL

    自定义标签:

    写个类

    package com.atguigu.javaweb.tag;
    
    import java.io.IOException;
    
    import javax.servlet.jsp.JspContext;
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.JspWriter;
    import javax.servlet.jsp.PageContext;
    import javax.servlet.jsp.tagext.JspFragment;
    import javax.servlet.jsp.tagext.JspTag;
    import javax.servlet.jsp.tagext.SimpleTagSupport;
    
    public class MaxTag extends SimpleTagSupport{
    
        private String num1;
        private String num2;
        
        public void setNum1(String num1) {
            this.num1 = num1;
        }
        
        public void setNum2(String num2) {
            this.num2 = num2;
        }
        
        @Override
        public void doTag() throws JspException, IOException {
            int a = 0;
            int b = 0;
            
            PageContext pageContext = (PageContext) getJspContext();
            
            JspWriter out = pageContext.getOut();
            
            try {
                a = Integer.parseInt(num1);
                b = Integer.parseInt(num2);
                out.print(a > b ? a : b);
            } catch (Exception e) {
                out.print("输入的属性的格式不正确!");
            }
            
        }
    
    }

    然后写tld文件

    <?xml version="1.0" encoding="UTF-8" ?>
    
    <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
        version="2.0">
    
        <!-- 描述 TLD 文件 -->
        <description>MyTag 1.0 core library</description>
        <display-name>MyTag core</display-name>
        <tlib-version>1.0</tlib-version>
    
        <!-- 建议在 JSP 页面上使用的标签的前缀 -->
        <short-name>atguigu</short-name>
        <!-- 作为 tld 文件的 id, 用来唯一标识当前的 TLD 文件, 多个 tld 文件的 URI 不能重复. 通过 JSP 页面的 taglib 
            标签的 uri 属性来引用. -->
        <uri>http://www.atguigu.com/mytag/core</uri>
    
        <tag>
            <name>readerFile</name>
            <tag-class>com.atguigu.javaweb.tag.ReadFileTag</tag-class>
            <body-content>empty</body-content>
    
            <attribute>
                <name>src</name>
                <required>true</required>
                <rtexprvalue>true</rtexprvalue>
            </attribute>
        </tag>
    
        <tag>
            <name>max</name>
            <tag-class>com.atguigu.javaweb.tag.MaxTag</tag-class>
            <body-content>empty</body-content>
    
            <attribute>
                <name>num1</name>
                <required>true</required>
                <rtexprvalue>true</rtexprvalue>
            </attribute>
    
            <attribute>
                <name>num2</name>
                <required>true</required>
                <rtexprvalue>true</rtexprvalue>
            </attribute>
        </tag>
    
        <!-- 描述自定义的 HelloSimpleTag 标签 -->
        <tag>
            <!-- 标签的名字: 在 JSP 页面上使用标签时的名字 -->
            <name>hello</name>
    
            <!-- 标签所在的全类名 -->
            <tag-class>com.atguigu.javaweb.tag.HelloSimpleTag</tag-class>
            <!-- 标签体的类型 -->
            <body-content>empty</body-content>
    
            <!-- 描述当前标签的属性 -->
            <attribute>
                <!-- 属性名 -->
                <name>value</name>
                <!-- 该属性是否被必须 -->
                <required>true</required>
                <!-- rtexprvalue: runtime expression value 当前属性是否可以接受运行时表达式的动态值 -->
                <rtexprvalue>true</rtexprvalue>
            </attribute>
    
            <attribute>
                <name>count</name>
                <required>false</required>
                <rtexprvalue>false</rtexprvalue>
            </attribute>
        </tag>
    
    </taglib>  

    在页面中使用:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    
    <!-- 导入标签库(描述文件) -->    
    <%@taglib uri="http://www.atguigu.com/mytag/core" prefix="atguigu" %>
        
    <!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>
    
        <atguigu:readerFile src="/WEB-INF/note.txt"/>
    
        <br><br>
        
        <atguigu:max num2="${param.a }" num1="${param.b }"/>
        
        <br>
        
        <atguigu:hello value="${param.name }" count="10"/>
        
    </body>
    </html>

    JSTL标签的各种使用:

      

    <%@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>
  • 相关阅读:
    传智博客.NET培训第13季 Ajax教程(共十三季) 学习资源
    一些sql语句的常用总结(重要)
    处理oracle的死锁
    Adroid 总结--android ListView美化,个性化更改的属性
    如何远程备份sql server数据库
    VSS (Visual Source Safe 2005) 用法详解
    php插入代码数据库
    PHP之PHP文件引用详解
    需要引入库:vue-resource
    axios调用详解
  • 原文地址:https://www.cnblogs.com/bulrush/p/6701972.html
Copyright © 2011-2022 走看看