准备资料
【tld示例】
<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instanse"
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>name</short-name>
</taglib>
需要建立三个文件.java .tld .jsp
java代码:
package com.zhiqi.testTaglib;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class TestTaglib extends TagSupport {
private static final long serialVersionUID = 1L;
@Override
public int doStartTag() throws JspException {
JspWriter jOut=this.pageContext.getOut();
try {
jOut.println("welcome");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return TagSupport.SKIP_BODY;
}
}
tld代码:
<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instanse"
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>nam</short-name>
<tag>
<name>myTag</name>
<tag-class>com.zhiqi.testTaglib.TestTaglib</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
jsp代码:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="myTa" uri="/WEB-INF/zhiqi.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>Test TagLib</title>
</head>
<body>
<h3>测试JSP自定义标签</h3>
<myTa:myTag/>
</body>
</html>
原理画图如下:
【带name属性的标签】
package com.zhiqi.testTaglib;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class TestTaglib extends TagSupport {
private static final long serialVersionUID = 1L;
private String name;
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
@Override
public int doStartTag() throws JspException {
JspWriter jOut=this.pageContext.getOut();
try {
jOut.println(name+" "+"welcome");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return TagSupport.SKIP_BODY;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instanse"
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>nam</short-name>
<tag>
<name>myTag</name>
<tag-class>com.zhiqi.testTaglib.TestTaglib</tag-class>
<body-content>empty</body-content>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
<myTa:myTag name="ooqq" />
【】