需要集成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方法进行赋值
剩下的大概也没啥了.就是继承一些接口,一些类即可,感觉和微信小程序的模板类似.