简单标签功能实例
来源:互联网 发布:网络吞吐量怎么计算 编辑:程序博客网 时间:2024/06/07 00:23
在JSP2.0以后,为了减少标签学习成本,SUN 公司推荐使用SimpleTag接口,这个接口的方法所有的对于标签的控件全部在doTag()这个标签里面,所以使用起来更加简单,下面列举简单标签实现的四个功能。
1.使用简单标签控制是否执行标签体
首先写一个类继承SimpleTagSupport这个实现类
package com.bird.web.simpleTag;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.JspFragment;import javax.servlet.jsp.tagext.SimpleTagSupport;/** * 控制标签内容是否显示 * @author bird * */public class SimpleTagDemo1 extends SimpleTagSupport{@Overridepublic void doTag() throws JspException, IOException {//JspFragment jf = this.getJspBody();//获得标签体的内容作为对象返回//jf.invoke(null);}}
在这个方法里面不需要执行任何方法,这样就不会执行标签体里面的内容了。
然后在tld描述文件里面对这个标签进行描述
<?xml version="1.0" encoding="UTF-8" ?><taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0"> <description>A tag library exercising SimpleTag handlers.</description> <tlib-version>1.0</tlib-version> <short-name>Bird</short-name> <uri>http://www.bird.com</uri> <tag> <name>demo1</name><tag-class>com.bird.web.simpleTag.SimpleTagDemo1</tag-class><body-content>scriptless</body-content> </tag> <tag> <name>demo2</name><tag-class>com.bird.web.simpleTag.SimpleTagDemo2</tag-class><body-content>scriptless</body-content> </tag> <tag> <name>demo3</name><tag-class>com.bird.web.simpleTag.SimpleTagDemo3</tag-class><body-content>scriptless</body-content> </tag> <tag> <name>demo4</name><tag-class>com.bird.web.simpleTag.SimpleTagDemo4</tag-class><body-content>empty</body-content> </tag> </taglib>
继续在页面里面使用这个标签
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%><%@taglib uri="http://www.bird.com" prefix="bird" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>使用简单标签控制是否执行标签体</title> </head> <body> <bird:demo1> aaaaa </bird:demo1> </body></html>
2.使用简单标签控制JSP页面是否显示
还是先是实现类
package com.bird.web.simpleTag;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.JspFragment;import javax.servlet.jsp.tagext.SimpleTagSupport;/** * * @author Bird * */public class SimpleTagDemo2 extends SimpleTagSupport{@Overridepublic void doTag() throws JspException, IOException {JspFragment jf = this.getJspBody();for(int i = 0; i < 5; i++){jf.invoke(null);}}}
然后是使用这个标签
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%><%@taglib uri="http://www.bird.com" prefix="bird" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>使用简单标签控制迭代数据</title> </head> <body> <bird:demo2> 小白 </bird:demo2> </body></html>
3.修改标签体内容
package com.bird.web.simpleTag;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;/** * 修改标签体 * @author Bird * */public class SimpleTagDemo3 extends SimpleTagSupport{@Overridepublic void doTag() throws JspException, IOException {JspFragment jf = this.getJspBody();StringWriter sw = new StringWriter();jf.invoke(sw);String content = sw.toString();content = content.toUpperCase();this.getJspContext().getOut().write(content);}}
然后使用这个标签
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%><%@taglib uri="http://www.bird.com" prefix="bird" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>修改标签体内容</title> </head> <body> <bird:demo3> aaaaaa </bird:demo3> </body></html>
4.使用简单标签控制JSP页面是否显示
package com.bird.web.simpleTag;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.SkipPageException;import javax.servlet.jsp.tagext.SimpleTagSupport;/** * 使用简单标签控制JSP页面是否显示 * @author Bird * */public class SimpleTagDemo4 extends SimpleTagSupport{@Overridepublic void doTag() throws JspException, IOException {throw new SkipPageException();}}然后使用这个标签
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%><%@taglib uri="http://www.bird.com" prefix="bird" %><bird:demo4/><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>使用简单标签控制JSP页面是否显示</title> </head> <body> </body></html>
- 简单标签功能实例
- 传统标签库功能实例
- 自定义标签简单实例---入门
- 【实例】赵雅智_简单标签实例
- JSP自定义标签之一:简单实例
- 简单标签的简介和实例运用
- 简单标签的简介和实例运用
- jsp自定义标签及简单实例
- 【自定义标签开发】04-简单标签库功能详解
- JEECG 简单实例讲解自定义表单功能
- 简单的自定义标签实现树结构的小功能
- JSF标签之a4j:mediaOutput 的最简单应用实例
- DIV+CSS系列课程 (三) 常用标签及简单实例
- HTML 简单教程-实例003 HTML段落<p>标签
- HTML 简单教程-实例004 HTML链接<a>标签
- HTML 简单教程-实例005 HTML图像<img> 标签
- 数据表格显示标签 - DisplayTag - 使用简单实例
- Thymeleaf 3.0 自定义标签方言属性,简单实例。快速学习!
- 关于二叉树,我们的中国特色
- 程序员面试之设计题
- 英语句子的否定转移
- Linux tar.gz zip 解压缩/压缩命令
- stun turn ice等穿越NAT方法
- 简单标签功能实例
- C#串口通信总结
- windows7安装myeclipse 10.0时出现failed to find a main class in "c:\windows\temp"解决
- Android中程序与Service交互的方式——综述
- 移动手机消息推送机制
- ubuntu 交叉编译环境的构建
- 海贼王游戏--EM游戏05--画龙点睛
- 查看语句运行时间异常的原因(SQLServer)
- 《大明宫词》之《采桑女》