JSP1.0中可以通过继承TagSupport或者BodyTagSupport来实现自定义的tag处理方法。

JSP2.0中也支持另外一种更为简单的自定tag的方法,那就是直接讲JSP代码保存成*.tag或者*.tagx的标签定义文件。tag和tagx文件不仅支持经典jsp代码,各种标签模版代码,还支持xml样式的jsp指令代码。

按照约定,tag和tagx文件需要放置在WEB-INF/tags目录下。

下面是一些简单的示例:

1.简单地显示时间time.tag

  1. <%@ tag import="java.util.*" import="java.text.*" %>
  2. <%
  3. DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
  4. Date d = new Date(System.currentTimeMillis());
  5. out.println(df.format(d));
  6. %>
  1. <%@ taglib prefix="util" tagdir="/WEB-INF/tags" %>
  2. <html>
  3. <head>
  4. </head>
  5. <body>
  6. Today is <util:time/>.
  7. </body>
  8. </html>

2.复制字符串多少遍repeater.tag

  1. <%@ attribute name="count" type="java.lang.Integer" required="true" %>
  2. <%@ attribute name="value" type="java.lang.String" required="true" %>
  3. <%!
  4. private String repeater(Integer count, String s) {
  5. int n = count.intValue();
  6. StringBuffer sb = new StringBuffer();
  7. for (int i = 0; i < n; i++) {
  8. sb.append(s);
  9. }
  10. return sb.toString();
  11. }
  12. %>
  13. <%
  14. out.println(repeater(count, value));
  15. %>
  1. <%@ taglib prefix="util" tagdir="/WEB-INF/tags" %>
  2. <html>
  3. <head>
  4. </head>
  5. <body>
  6. Let's get some sleep! <util:repeater count='${3 * 10}' value='zzz'/>
  7. </body>
  8. </html>

3.查找省份lookup.tag

  1. <%@ tag import="java.util.*" %>
  2. <%@ attribute name="cityName" required="true" %>
  3. <%@ variable name-given="province" %>
  4. <%@ variable name-given="population" variable-class="java.lang.Integer" %>
  5. <%
  6. if ("Toronto".equals(cityName)) {
  7. jspContext.setAttribute("province", "Ontario");
  8. jspContext.setAttribute("population", new Integer(2553400));
  9. }
  10. else if ("Montreal".equals(cityName)) {
  11. jspContext.setAttribute("province", "Quebec");
  12. jspContext.setAttribute("population", new Integer(2195800));
  13. }
  14. else {
  15. jspContext.setAttribute("province", "Unknown");
  16. jspContext.setAttribute("population", new Integer(-1));
  17. }
  18. %>
  19. <jsp:doBody/>
  1. <%@ taglib prefix="util" tagdir="/WEB-INF/tags" %>
  2. <html>
  3. <head>
  4. </head>
  5. <body>
  6. <% pageContext.setAttribute("cityName", "Montreal"); %>
  7. <util:lookup cityName="${cityName}">
  8. ${cityName}'s province is ${province}.
  9. ${cityName}'s population is approximately ${population / 1000000} million.
  10. </util:lookup>
  11. </body>
  12. </html>

上面的都是使用的经典jsp代码,下面将第3个示例使用其他代码实现:

*使用标签:

  1. <%@ tag import="java.util.*" %>
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  3. <%@ attribute name="cityName" required="true" %>
  4. <%@ variable name-given="province" %>
  5. <%@ variable name-given="population" %>
  6. <c:choose>
  7. <c:when test="cityName eq 'Toronto'>
  8. <c:set var="province" value="Ontario"/>
  9. <c:set var="population" value="2553400"/>
  10. </c:when>
  11. <c:when test="cityName eq 'Montreal'>
  12. <c:set var="province" value="Quebec"/>
  13. <c:set var="population" value="2195800"/>
  14. </c:when>
  15. <c:otherwise>
  16. <c:set var="province" value="Unknown"/>
  17. <c:set var="population" value="-1"/>
  18. </c:otherwise>
  19. </c:choose>
  20. %>
  21. <jsp:doBody/>
  1. <%@ taglib prefix="util" tagdir="/WEB-INF/tags" %>
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  3. <html>
  4. <head>
  5. </head>
  6. <body>
  7. <c:set var="cityName" value="Montreal"/>
  8. <util:lookup cityName="${cityName}">
  9. ${cityName}'s province is ${province}.
  10. ${cityName}'s population is approximately ${population / 1000000} million.
  11. </util:lookup>
  12. </body>
  13. </html>

*使用jsp指令,通常这种方式生成xml格式的文件

  1. <?xml version='1.0'?>
  2. <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page">
  3. <jsp:directive.tag import="java.util.*"/>
  4. <jsp:directive.taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"/>
  5. <jsp:directive.attribute name="cityName" required="true"/>
  6. <jsp:directive.variable name-given="province"/>
  7. <jsp:directive.variable name-given="population"/>
  8. <c:choose>
  9. <c:when test="cityName eq 'Toronto'>
  10. <c:set var="province" value="Ontario"/>
  11. <c:set var="population" value="2553400"/>
  12. </c:when>
  13. <c:when test="cityName eq 'Montreal'>
  14. <c:set var="province" value="Quebec"/>
  15. <c:set var="population" value="2195800"/>
  16. </c:when>
  17. <c:otherwise>
  18. <c:set var="province" value="Unknown"/>
  19. <c:set var="population" value="-1"/>
  20. </c:otherwise>
  21. </c:choose>
  22. </jsp:root>
  1. <?xml version='1.0'?>
  2. <jsp:root version='2.0'
  3. xmlns:jsp="http://java.sun.com/JSP/Page"
  4. xmlns:util="urn:jsptagdir:/WEB-INF/tags"
  5. xmlns:c="http://java.sun.com/jsp/jstl/core">
  6. <jsp:directive.page contentType="text/html"/>
  7. <html>
  8. <head>
  9. </head>
  10. <body>
  11. <c:set var="cityName" value="Montreal"/>
  12. <util:lookup cityName="${cityName}">
  13. ${cityName}'s province is ${province}.
  14. ${cityName}'s population is approximately ${population / 1000000} million.
  15. </util:lookup>
  16. </body>
  17. </html>
  18. </jsp:root>

附录:

标签文件中常用的指令:

tag 类似JSP page指令,可以用于import常用的java类库等
include 导入其他的标签定义文件
taglib 使用其他标签,如jstl, spring tag, struts tag等等
attribute 定义一个属性
variable 定义一个jsp page中可见的变量,默认范围为NESTED,表示标签内有效。可选项有AT_BEGIN和AT_END

这些指令的可选属性

body-content 标签body的处理方式 ,可选项: 'empty', 'tagdependent' or 'scriptless'
import 导入使用的java类库
pageEncoding 设置页面编码
isELIgnored 是否忽略el表达式
dynamic-attributes 用于存储自定义属性的map,所谓的自定义属性指:未隐式申明的变量
language 使用的脚本语言,目前必须是java
display-name 标签名
small-icon for tools
large-icon for tools
description 标签作用描述
example informal description of how the tag is used

<jsp:directive:attribute>的可选属性

name 属性名
required true or false
rtexprvalue true or false - 指定是否支持运行时表达式
type 值类型 - 默认是java.lang.String
fragment true or false - 值先传递给容器(false), 直接传给标签处理方法(true)
description 属性描述

<jsp:directive:variable>的可选属性

name-given 变量名(标签使用时的变量名)
name-from-attribute Specifies the name of an attribute, whose value is the name of the variable that will be available in the calling JSP page. Exactly one of name-given or name-from-attribute must be supplied.
alias A locally scoped variable which will store the variable's value. Used only with name-from-attribute.
variable-class 变量类.默认是java.lang.String.
declare Indicates whether the variable is declared in the calling JSP page or tag file. Default is true. Not entirely clear what this means!
scope 变量范围,可选项 AT_BEGIN(标签后jsp page内有效), AT_END(标签后jsp page内有效) and NESTED. NESTED(默认,标签内有效)
description 变量描述

自定义tag标签的方法的更多相关文章

  1. 自定义tag标签-实现long类型转换成Date类型

    数据库里存储的是bigint型的时间,entity实体中存放的是long类型的标签,现在想输出到jsp页面,由于使用的是jstl标签,而要显示的是可读的时间类型,找来找去有个 fmt:formatDa ...

  2. Jsp 自定义tag标签

    1转自:https://blog.csdn.net/yusimiao/article/details/46835617 Jsp自定义tag标签 自定义tag标签的好处 程序员可以自定一些特定功能的标记 ...

  3. struts2 自定义tag标签

    在项目中可能有很多相同的jsp页面表示功能,这时可以使用自定义的tag进行定义,渐少重复的工作量便于日后维护! 下面就基于struts2进行自定义标签的定义与实现: 首先:自定义类MyTag继承str ...

  4. jsp如何自定义tag的标签库?

    虽然和上一次的使用自定义的tld标签简化jsp的繁琐操作的有点不同,但是目的也是一致的.自定义tag比较简单. 1.新建tag标签 在WEB-INF目录下新建一个tags的文件夹,是自定义tag标签的 ...

  5. Inno Setup技巧[界面]添加和自定义左下角标签

    原文 http://blog.sina.com.cn/s/blog_5e3cc2f30100cc49.html 本文介绍添加和自定义“左下角标签”的方法. 界面预览: Setup技巧[界面]添加和自定 ...

  6. 6.1 如何在spring中自定义xml标签

    dubbo自定义了很多xml标签,例如<dubbo:application>,那么这些自定义标签是怎么与spring结合起来的呢?我们先看一个简单的例子. 一 编写模型类 package ...

  7. (转) ThinkPHP模板自定义标签使用方法

    这篇文章主要介绍了ThinkPHP模板自定义标签使用方法,需要的朋友可以参考下  转之--http://www.jb51.net/article/51584.htm   使用模板标签可以让网站前台开发 ...

  8. 如何自定义JSTL标签与SpringMVC 标签的属性中套JSTL标签报错的解决方法

    如何自定义JSTL标签 1.创建一个类,从SimpleTagSupport继承 A) 通过继承可以获得当前JSP页面上的对象,如JspContext I) 实际上可以强转为PageContext II ...

  9. JSP自定义tag控件标签

    JSP支持自定tag的方法,那就是直接讲JSP代码保存成*.tag或者*.tagx的标签定义文件.tag和tagx文件不仅支持经典jsp代码,各种标签模版代码,还支持xml样式的jsp指令代码. 按照 ...

随机推荐

  1. python(一):python语言基础

    一.python语言基本的8个要素 Python语言的8个要素:数据类型.对象引用.组合数据类型.逻辑操作符.运算操作符.控制流语句.输入/输出.函数的创建与引用.除此之外还有一个非常重要且无处不在的 ...

  2. CentOS解压rar文件

    默认不能解压rar文件. 进官网下载:http://www.rarsoft.com/download.htm RAR 5.40 for Linux x64 安装: # tar -zxvf rarlin ...

  3. 【jQuery插件分享】Cropper——一个简单方便的图片裁剪插件

    原文:https://segmentfault.com/a/1190000012344970 插件介绍 这是一个我在写以前的项目的途中发现的一个国人写的jQuery图像裁剪插件,当时想实现用户资料的头 ...

  4. 自定义vue全局组件use使用(解释vue.use()的原理)

    我们在前面学习到是用别人的组件:Vue.use(VueRouter).Vue.use(Mint)等等.其实使用的这些都是全剧组件,这里我们就来讲解一下怎么样定义一个全局组件,并解释vue.use()的 ...

  5. poj 1637 Sightseeing tour——最大流+欧拉回路

    题目:http://poj.org/problem?id=1637 先给无向边随便定向,如果一个点的入度大于出度,就从源点向它连 ( 入度 - 出度 / 2 ) 容量的边,意为需要流出去这么多:流出去 ...

  6. MyEclipse中将普通Java项目convert(转化)为Maven项目

    在MyEclipse10中将Maven项目转成普通Java项目后,想将Java项目转成Maven项目,结果一下子傻眼了.根本就没有攻略中提到的config标签.仔细一看,喵咪的,人家用的是Eclips ...

  7. C语言(C99标准)在结构体的初始化上与C++的区别

    C++中由于有构造函数的概念,所以很多时候初始化工作能够很方便地进行,而且由于C++标准库中有很多实用类(往往是类模板),现代C++能十分容易地编写. 比如现在要构造一个类Object,包含两个字段, ...

  8. Js中常用的字符串,数组,函数扩展

    由于最近辞职在家,自己的时间相对多一点.所以就根据prototytpeJS的API,结合自己正在看的司徒大神的<javascript框架设计>,整理了下Js中常用一些字符串,数组,函数扩展 ...

  9. ffmpeg 基本数据结构和对象: AVPacket、AVPicture、AVFrame

    一.AVPacket /** * AVPacket 作为解码器的输入 或 编码器的输出. * 当作为解码器的输入时,它由demuxer生成,然后传递给解码器 * 当作为编码器的输出时,由编码器生成,然 ...

  10. Sql--------服务器的数据库表数据插入到本地数据库

    本地语句:::insert into 表名(列名) SELECT * FROM OPENDATASOURCE('SQLOLEDB', 'Data Source=127.0.0.1;User ID=sa ...