JSP 标签编程

栏目: JSP · 发布时间: 6年前

内容简介:需要集成TagSupport类,并覆写doStartTage方法然后需要再次定义标签描述文件,即web-inf文件下的helloTage.tld文件最后,在页面引入相关内容

空标签

需要集成TagSupport类,并覆写doStartTage方法

package com.ming.TagDome;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class HelloTag extends TagSupport {
    @Override
    public int doStartTag() throws JspException {
        // 获得输出流对象
        JspWriter out = super.pageContext.getOut();
        try{
            out.println("<h2>hello world</h2>");
        }catch (Exception e){
            e.printStackTrace();
        }
        // 没有便签体
        return TagSupport.SKIP_BODY;
    }
}

然后需要再次定义标签描述文件,即web-inf文件下的helloTage.tld文件

<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="jar:///snap/intellij-idea-ultimate/122/plugins/jsp/lib/jsp-impl.jar!/standardSchemas/jspdirectives.xsd"
    version="2.1"
>
    <tlib-version>1.0</tlib-version>
    <short-name>firsttag</short-name>
    <tag>
        <name>hello</name>
        <tag-class>
            com.ming.TagDome.HelloTag
        </tag-class>
        <body-content>empty</body-content>
    </tag>
</taglib>

最后,在页面引入相关内容

<%--
  Created by IntelliJ IDEA.
  User: ming
  Date: 19-3-20
  Time: 下午10:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="myTag" uri="/WEB-INF/hellotag.tld"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <myTag:hello/>
</body>
</html>

进行文件映射

在web.xml文件中对所有的tld文件进行映射.

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
        <taglib>
            <taglib-uri>hello_tag</taglib-uri>
            <taglib-location>/WEB-INF/hellotag.tld</taglib-location>
        </taglib>
    <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/ming</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
</web-app>

运行原理 访问的时候,如果遇到标签,则会根据uri去寻找对应的配置文件,根据配置文件,读取相应的标签类class,然后,进行输出

定义有属性的标签

一个栗子,格式化日期标签类

package com.ming.TagDome;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTag  extends TagSupport {
    // 接收魔板
    private String format;
    @Override
    public int doStartTag() throws JspException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(this.format);
        try{
            // 输出格式化后的日期
            super.pageContext.getOut().write(simpleDateFormat.format(new Date()));
        }catch (IOException e){
            e.printStackTrace();
        }
        return TagSupport.SKIP_BODY;
    }

    public String getFormat(){
        return format;
    }

    public void setFormat(String _format){
        this.format = _format;
    }
}
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="jar:///snap/intellij-idea-ultimate/122/plugins/jsp/lib/jsp-impl.jar!/standardSchemas/jspdirectives.xsd"
        version = "2.1">
    <tlib-version>1.0</tlib-version>
    <short-name>datetag</short-name>
    <tag>
        <name>date</name>
        <tag-class>
            com.ming.TagDome.DateTag
        </tag-class>
        <body-content>empty</body-content>
        <attribute>
            <!-- 数据初始化 -->
            <name>format</name>
            <required>true</required>
            <rtxprvalue>true</rtxprvalue>
        </attribute>
    </tag>
</taglib>
<%--
  Created by IntelliJ IDEA.
  User: ming
  Date: 19-3-20
  Time: 下午10:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="myTag" uri="ming_date"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<myTag:date format="yyy-MM-dd HH:mm:ss:SSS"/>
</body>
</html>

在设置标签属性的时候,属性为format的时候,会自动调用set方法进行赋值

剩下的大概也没啥了.就是继承一些接口,一些类即可,感觉和微信小程序的模板类似.


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Powerful

Powerful

Patty McCord / Missionday / 2018-1-25

Named by The Washington Post as one of the 11 Leadership Books to Read in 2018 When it comes to recruiting, motivating, and creating great teams, Patty McCord says most companies have it all wrong. Mc......一起来看看 《Powerful》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换