JSP2.x自定义标签的创建
来源:互联网 发布:购买淘宝网店铺 编辑:程序博客网 时间:2024/05/01 04:54
不同于JSP1.x,2.0开始,JSP提供了一套简化的标签,SimpleTag()接口,实现doTag()方法。
示例:
1、不带标签体的标签
taglib.tld配置
<tag> <name>table</name> <tagclass>my.Table</tagclass> <bodycontent>JSP</bodycontent> <info>Love Rabbit So Much.</info> <attribute> <name>items</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>url</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag>
MultiTag.java 标签实现类
package my;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;public class MultiTag extends SimpleTagSupport{private int num1;private int num2;@Overridepublic void doTag() throws JspException, IOException{this.getJspContext().getOut().write("" + num1 + " * " + num2 + " = " + (num1 * num2));}public int getNum1(){return num1;}public void setNum1(int num1){this.num1 = num1;}public int getNum2(){return num2;}public void setNum2(int num2){this.num2 = num2;}}
multi.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%><%@ taglib uri="http://www.loverabbit.com/tags" prefix="taglib"%><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body> <taglib:multi num1="3" num2="4" /></body></html>
运行结果:
2、带标签体的标签
taglib.tld配置
<tag> <name>toUpperCase</name> <tagclass>my.ToUpperCaseTag</tagclass> <bodycontent>tagdependent</bodycontent> <info>Love Rabbit So Much.</info> </tag>
ToUpperCaseTag.java 标签实现类
package my;import java.io.IOException;import java.io.StringWriter;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.JspFragment;import javax.servlet.jsp.tagext.SimpleTagSupport;public class ToUpperCaseTag extends SimpleTagSupport{@Overridepublic void doTag() throws JspException, IOException{// 将 标签体内容读入该 writerStringWriter writer = new StringWriter();// 标签体 为 JspFragment 的形式JspFragment jspFragment = this.getJspBody();// 通过 invoke 输出到指定的 writer 中。// 如果参数为 null,将输出到默认的 writer 中,即 getJspContext().getOut() 输出到HTML中jspFragment.invoke(writer);String content = writer.getBuffer().toString();this.getJspContext().getOut().print(content.toUpperCase());}}
toUpperCase.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%><%@ taglib uri="http://www.loverabbit.com/tags" prefix="taglib"%><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body> <taglib:toUpperCase>This is a to upper case tag. </taglib:toUpperCase></body></html>
运行结果:
3、带多个标签体的标签
taglib.tld配置
<tag> <name>multiAttribute</name> <tagclass>my.MultiAttributeTag</tagclass> <bodycontent>tagdependent</bodycontent> <info>Love Rabbit So Much.</info> <attribute> <name>body1</name> <required>false</required> <fragment>true</fragment> </attribute> <attribute> <name>body2</name> <required>false</required> <fragment>true</fragment> </attribute> </tag>
MultiAttributeTag.java 标签实现类
package my;import java.io.IOException;import java.io.StringWriter;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.JspFragment;import javax.servlet.jsp.tagext.SimpleTagSupport;public class MultiAttributeTag extends SimpleTagSupport{private JspFragment body1;private JspFragment body2;public void setBody1(JspFragment body1){this.body1 = body1;}public void setBody2(JspFragment body2){this.body2 = body2;}@Overridepublic void doTag() throws JspException, IOException{StringWriter writer1 = new StringWriter();StringWriter writer2 = new StringWriter();for(int i = 0; i < 5; i++){// body1 调用 5 次body1.invoke(writer1);}for(int i = 0; i < 3; i++){// body2 调用 3 次body2.invoke(writer2);}this.getJspContext().getOut().print("3 次调用 body2 后的结果:" + writer2.getBuffer().toString()+ "<br/><br/>");this.getJspContext().getOut().print("5 次调用 body1 后的结果:" + writer1.getBuffer().toString()+ "<br/><br/>");}}
multiAttribute.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%><%@ taglib uri="http://www.loverabbit.com/tags" prefix="taglib"%><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><style type="text/css">body {font-size: 12px;}</style></head><body> <taglib:multiAttribute><jsp:attribute name="body1">标签体一, </jsp:attribute><jsp:attribute name="body2">标签体二, </jsp:attribute></taglib:multiAttribute></body></html>
运行结果:
0 0
- JSP2.x自定义标签的创建
- JSP2的自定义标签
- JSP2的自定义标签
- JSP2的自定义标签
- jsp2的自定义标签
- jsp2的自定义标签
- JSP2的自定义标签详解
- 一,JSP2的自定义标签
- JSP2自定义标签的简单示例
- JSP/Servlet-----10、JSP2的自定义标签
- JSP2的自定义标签(2)
- JSP2.0自定义标签
- jsp2自定义标签
- JSP2-自定义标签
- JSP2.0自定义标签
- JSP学习笔记(六):自定义标签-JSP2.x
- 深入分析JavaWeb Item24 -- jsp2.X自定义标签开发进阶
- 深入分析JavaWeb 24 -- jsp2.X自定义标签开发进阶
- Android使用内容提供者方式进行存储
- 测试一下
- 虚拟键码对照表
- Tyvj P1015 公路乘车 (DP)
- 待字闺中之兄弟数字分析
- JSP2.x自定义标签的创建
- Direct2D随笔4——GameInput
- 嘟嘟
- Linux mutex相关知识
- android 多线程
- windows下Eclipse启动tomcat提示端口已被占用 already in use
- Android O/R Mapping 框架
- Android获取屏幕分辨率及dp与 pix间的转换
- HTTP状态代码(各种错误代码集合)