JSP自定义标签
2013-12-12 21:25
633 查看
自定义标签
1.编写标签处理类,继承SimpleTagSupport
package mytag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
publicclass HelloTag extendsSimpleTagSupport{
@Override
publicvoiddoTag() throws JspException, IOException {
this.getJspContext().getOut().write("Hello World");
}
}
2.标签处理类必须在包里,不能是裸体类
3.在WEB-INF目录或子目录下建立一个helloworld.tld
<?xmlversion="1.0"encoding="UTF-8"?>
<taglibxmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"version="2.0"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"> <tlib-version>1.0</tlib-version>
<short-name>mytag</short-name>
<uri>/helloworldtaglib</uri>
<tag>
<name>helloworld</name>
<tag-class>mytag.HelloTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
4.建立testTag.jsp
<%@pagelanguage="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglibprefix="mytag"uri="/helloworldtaglib"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<mytag:helloworld/>
</body>
</html>
实例二
1.WEB-INF/product.tld
<?xmlversion="1.0"encoding="UTF-8"?>
<taglibxmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"version="2.0"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"> <tlib-version>1.0</tlib-version>
<short-name>product</short-name>
<uri>/producttaglib</uri>
<tag>
<name>list</name>
<tag-class>mytag.ProductTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
2.
package mytag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
publicclass ProductTag extends SimpleTagSupport{
@Override
publicvoid doTag() throws JspException, IOException {
this.getJspContext().getOut().write("**********");
}
}
3.
<%@pagelanguage="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglibprefix="mytag"uri="/helloworldtaglib"%>
<%@taglibprefix="product"uri="/producttaglib"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<mytag:helloworld/>
<product:list/>
</body>
</html>
1.编写标签处理类,继承SimpleTagSupport
package mytag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
publicclass HelloTag extendsSimpleTagSupport{
@Override
publicvoiddoTag() throws JspException, IOException {
this.getJspContext().getOut().write("Hello World");
}
}
2.标签处理类必须在包里,不能是裸体类
3.在WEB-INF目录或子目录下建立一个helloworld.tld
<?xmlversion="1.0"encoding="UTF-8"?>
<taglibxmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"version="2.0"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"> <tlib-version>1.0</tlib-version>
<short-name>mytag</short-name>
<uri>/helloworldtaglib</uri>
<tag>
<name>helloworld</name>
<tag-class>mytag.HelloTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
4.建立testTag.jsp
<%@pagelanguage="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglibprefix="mytag"uri="/helloworldtaglib"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<mytag:helloworld/>
</body>
</html>
实例二
1.WEB-INF/product.tld
<?xmlversion="1.0"encoding="UTF-8"?>
<taglibxmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"version="2.0"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"> <tlib-version>1.0</tlib-version>
<short-name>product</short-name>
<uri>/producttaglib</uri>
<tag>
<name>list</name>
<tag-class>mytag.ProductTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
2.
package mytag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
publicclass ProductTag extends SimpleTagSupport{
@Override
publicvoid doTag() throws JspException, IOException {
this.getJspContext().getOut().write("**********");
}
}
3.
<%@pagelanguage="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglibprefix="mytag"uri="/helloworldtaglib"%>
<%@taglibprefix="product"uri="/producttaglib"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<mytag:helloworld/>
<product:list/>
</body>
</html>
相关文章推荐
- JSP自定义标签的实现
- JSP自定义标签
- jsp自定义标签
- 详细讲解了 JSP自定义标签 的 接口和 方法 原理
- jsp页面中自定义标签的小演示
- JSP自定义标签详解
- JSP GridView --使用自定义标签实现ASP.NET的控件
- jsp自定义标签封装(tld)
- JSP自定义标签(一到八)
- Jsp自定义标签第一次课内容
- JSP自定义标签---->张国亮总结第一季
- Rhythmk 一步一步学 JAVA(7): jsp 自定义标签
- jsp自定义标签晋级【实现循环输出】
- jsp自定义标签实现权限拦截
- jsp学习3-自定义标签
- 自定义标签(JspFragment类、invoke方法、开发带属性的标签)
- Struts2 自定义标签(JSP视图)实现图形验证功能
- 使用自定义标签,将结果集返回到jsp页面!(推荐方法)
- (转)掌握jsp自定义标签:(四)
- jsp使用自定义标签taglib分页系列——ColumneditTag.java