更新时间:2016年09月29日16时59分 来源:传智播客JAVA培训学院 浏览次数:
public class MyTag implements SimpleTag{ private PageContext pageContext; private JspFragment body; @Override public void doTag() throws JspException, IOException { //向页面中输出内容 pageContext.getOut().print("hello tag"); } @Override public JspTag getParent() { return null; } @Override public void setJspBody(JspFragment jspBody) { this.body=jspBody; } @Override public void setJspContext(JspContext pc) { this.pageContext=(PageContext) pc;//将参数pc赋值给成员变量pageContext } public void setParent(JspTag parent) {} } |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>itcast</short-name> <uri>http://www.itcast.cn/tags/it-1.0</uri> </taglib> |
<?xml version="1.0" encoding="UTF-8"?> <taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" version="2.1"> <description>JSTL 1.1 core library</description> <display-name>JSTL core</display-name> <tlib-version>1.1</tlib-version> <short-name>c</short-name> <uri>http://java.sun.com/jsp/jstl/core</uri> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>itcast</short-name> <uri>http://www.itcast.cn/tags/it-1.0</uri> </taglib> |
<?xml version="1.0" encoding="UTF-8"?> <taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" version="2.1"> <tlib-version>1.0</tlib-version> <short-name>itcast</short-name> <uri>http://www.itcast.cn/tags/it-1.0</uri> </taglib> |
<?xml version="1.0" encoding="UTF-8"?> <taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" version="2.1"> <tlib-version>1.0</tlib-version> <short-name>itcast</short-name> <uri>http://www.itcast.cn/tags/it-1.0</uri> <tag> <name>MyTag</name> <tag-class>cn.itcast.tag.MyTag</tag-class> <body-content>empty</body-content> </tag> </taglib> |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="it" uri="/WEB-INF/tld/itcast-tag.tld" %> <!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> <it:MyTag/> </body> </html> |
public class MyTag1 extends SimpleTagSupport{ @Override public void doTag() throws JspException, IOException { this.getJspContext().getOut().print("继承SimpleTagSupport类的标签"); } } |
此处省略例1-5中的代码 <tag> <name>MyTag1</name> <tag-class>cn.itcast.tag.MyTag1</tag-class> <body-content>empty</body-content> </tag> |
此处省略例1-6中的代码 <br> SimpleTagSupport:<it:MyTag1/> |