1、项目结构
2、实现自定义tag所需依赖
<dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency>
3、HelloTag.java
package com.tag; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.SimpleTagSupport; import java.io.IOException; /** * Created by Administrator on 2016/7/11. */ public class HelloTag extends SimpleTagSupport { @Override public void doTag() throws JspException, IOException { JspWriter out = getJspContext().getOut(); out.println("Hello Custom Tag!"); } }
4、costom.tld
<taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>Example TLD</short-name> <tag> <name>Hello</name> <tag-class>com.tag.HelloTag</tag-class> <body-content>empty</body-content> </tag> </taglib>
5、index.jsp
<%@ taglib prefix="ex" uri="../WEB-INF/custom.tld"%> <html> <body> <h2>Hello World!</h2> <form action="test1" method="post" id="form1"> <input type="submit" name="submit" value="button"> </form> <ex:Hello></ex:Hello> </body> </html>
注:这里需要引入标签库
<%@ taglib prefix="ex" uri="../WEB-INF/custom.tld"%>
6、结果