zoukankan      html  css  js  c++  java
  • JSP标签编程--简单标签

    javax.servlet.jsp.tagext里的类SimpleTagSupport

    使用SimpleTagSupport类一网打尽以往复杂的标签开发,直接使用doTag()方法

    java文件:

    package org.lxh.tagdemo ;
    import java.io.* ;
    import java.util.* ;
    import java.text.* ;
    import javax.servlet.jsp.* ;
    import javax.servlet.jsp.tagext.* ;
    public class SimpleDateTag extends SimpleTagSupport {
        private String format ;    // 接收格式化
        public void doTag()
               throws JspException,
                      IOException{
            SimpleDateFormat sdf = new SimpleDateFormat(this.format) ;
            try{
                super.getJspContext().getOut().write(sdf.format(new Date())) ;
            }catch(Exception e){}
        }
        public void setFormat(String format){
            this.format = format ;
        }
        public String getFormat(){
            return this.format ;
        }
    }

      tlb文件:

    <?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_1.xsd"
        version="2.1">
        <tlib-version>1.0</tlib-version>
        <short-name>mldntag</short-name>
       
        <tag>
        <name>simpledate</name>
        <tag-class>org.lxh.tagdemo.SimpleDateTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>format</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        </tag>
    </taglib>

    web.xml文件:

    <jsp-config>
            <taglib>
                <taglib-uri>mldn</taglib-uri>
                <taglib-location>/WEB-INF/mldntag.tld</taglib-location>
            </taglib>
        </jsp-config>

    jsp文件:

    <%@ page contentType="text/html" pageEncoding="GBK"%>
    <%@ taglib prefix="mytag" uri="mldn"%>
    <html>
    <head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
    <body>
        <h1>
            <mytag:simpledate format="yyyy-MM-dd HH:mm:ss.SSS"/>
        </h1>
    </body>
    </html>

     定义有属性的标签:

    java 文件:format作为属性

    package org.lxh.tagdemo ;
    import java.text.* ;
    import java.util.* ;
    import javax.servlet.jsp.* ;
    import javax.servlet.jsp.tagext.* ;
    public class DateTag extends TagSupport {
        private String format ;    // 当设置属性的时候可以通过setter完成
        public int doStartTag()
                   throws JspException{
            SimpleDateFormat sdf = new SimpleDateFormat(this.format) ;
            // 表示进行格式化的日期显示操作
            try{
                super.pageContext.getOut().write(sdf.format(new Date())) ;
            }catch(Exception e){
                e.printStackTrace() ;    // 异常处理操作
            }
            return TagSupport.SKIP_BODY ;
        }
        public void setFormat(String format){
            this.format = format ;
        }
        public String getFormat(){
            return this.format ;
        }
    }

    tld文件,定义属性format:

    <?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_1.xsd"
        version="2.1">
        <tlib-version>1.0</tlib-version>
        <short-name>datetag</short-name>
        <tag>
        <name>date</name>
        <tag-class>org.lxh.tagdemo.DateTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>format</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        </tag>
    </taglib>

    web.xml:

    <jsp-config>
            <taglib>
                <taglib-uri>mldn_date</taglib-uri>
                <taglib-location>/WEB-INF/datetag.tld</taglib-location>
            </taglib>
        </jsp-config>

    JSP调用文件,format的setter通过反射机制完成:

    <%@ page contentType="text/html" pageEncoding="GBK"%>
    <%@ taglib prefix="mytag" uri="mldn_date"%>
    <html>
    <head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
    <body>
        <h1><mytag:date format="yyyy-MM-dd HH:mm:ss.SSS"/></h1>
    </body>
    </html>
  • 相关阅读:
    java基础知识--环境变量配置
    安装oracle11g时遇到INS-13001环境不满足最低要求
    MINA系列学习-IoBuffer
    MINA系列学习-mina整体介绍
    DBCP数据源连接池实现原理分析
    dbcp数据源配置杂谈
    Java 内存区域和GC机制分析
    网站的防盗链与反盗链的那点事
    这一天博客小院我进来了!
    AOP
  • 原文地址:https://www.cnblogs.com/wujixing/p/5010491.html
Copyright © 2011-2022 走看看