日期:2014-05-16 浏览次数:20505 次
package mytag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class HelloJSP_Taglib extends SimpleTagSupport { //自定义Tag
@Override
public void doTag() throws JspException, IOException {
this.getJspContext().getOut().write("This is my first Taglib programme.<br>");
this.getJspContext().getOut().write("Use the Taglib you can do something like the "
+ "Mmakeup Language.<br>");
}
}
<?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-jsptalib_2_5.xsd" version="2.0"> <tlib-version>1.0</tlib-version> <short-name>mytag</short-name> <uri>/hellojsptaglib</uri> <tag> <name>HelloJSP_Taglib</name> <tag-class>mytag.HelloJSP_Taglib</tag-class> <body-content>empty</body-content></tag> </taglib>
<%@ page language="java" import="mytag.*" pageEncoding="GB18030"%>
<%@ taglib uri="/hellojsptaglib" prefix="mytag" %>
<%@ taglib uri="/productlisttaglib" prefix="product" %>
<!-- 注意这里uri要与上面的HelloJSP_Taglib.tld 文件里的uri 内容保持一致 -->
<!-- prefix 内容要与HelloJSP_Taglib.tld 文件里的short-name 内容保持一致 -->
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+
":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'TestTaglib.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<mytag:HelloJSP_Taglib/> <!-- 这里使用了自己定义的Tag -->
</center>
</body>
</html>