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

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

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

下面是一些简单的示例:

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

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

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

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

3.查找省份lookup.tag

<%@ tag import="java.util.*" %>
<%@ attribute name="cityName" required="true" %>
<%@ variable name-given="province" %>
<%@ variable name-given="population" variable-class="java.lang.Integer" %>
<%
if ("Toronto".equals(cityName)) {
jspContext.setAttribute("province", "Ontario");
jspContext.setAttribute("population", new Integer(2553400));
}
else if ("Montreal".equals(cityName)) {
jspContext.setAttribute("province", "Quebec");
jspContext.setAttribute("population", new Integer(2195800));
}
else {
jspContext.setAttribute("province", "Unknown");
jspContext.setAttribute("population", new Integer(-1));
}
%>
<jsp:doBody/>
<%@ taglib prefix="util" tagdir="/WEB-INF/tags" %>
<html>
<head>
</head>
<body>
<% pageContext.setAttribute("cityName", "Montreal"); %>
<util:lookup cityName="${cityName}">
${cityName}'s province is ${province}.
${cityName}'s population is approximately ${population / 1000000} million.
</util:lookup>
</body>
</html>

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

*使用标签:

<%@ tag import="java.util.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ attribute name="cityName" required="true" %>
<%@ variable name-given="province" %>
<%@ variable name-given="population" %>
<c:choose>
<c:when test="cityName eq 'Toronto'>
<c:set var="province" value="Ontario"/>
<c:set var="population" value="2553400"/>
</c:when>
<c:when test="cityName eq 'Montreal'>
<c:set var="province" value="Quebec"/>
<c:set var="population" value="2195800"/>
</c:when>
<c:otherwise>
<c:set var="province" value="Unknown"/>
<c:set var="population" value="-1"/>
</c:otherwise>
</c:choose>
%>
<jsp:doBody/>
<%@ taglib prefix="util" tagdir="/WEB-INF/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
</head>
<body>
<c:set var="cityName" value="Montreal"/>
<util:lookup cityName="${cityName}">
${cityName}'s province is ${province}.
${cityName}'s population is approximately ${population / 1000000} million.
</util:lookup>
</body>
</html>

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

<?xml version='1.0'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page">
<jsp:directive.tag import="java.util.*"/>
<jsp:directive.taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"/>
<jsp:directive.attribute name="cityName" required="true"/>
<jsp:directive.variable name-given="province"/>
<jsp:directive.variable name-given="population"/>
<c:choose>
<c:when test="cityName eq 'Toronto'>
<c:set var="province" value="Ontario"/>
<c:set var="population" value="2553400"/>
</c:when>
<c:when test="cityName eq 'Montreal'>
<c:set var="province" value="Quebec"/>
<c:set var="population" value="2195800"/>
</c:when>
<c:otherwise>
<c:set var="province" value="Unknown"/>
<c:set var="population" value="-1"/>
</c:otherwise>
</c:choose>
</jsp:root>
<?xml version='1.0'?>
<jsp:root version='2.0'
xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:util="urn:jsptagdir:/WEB-INF/tags"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<jsp:directive.page contentType="text/html"/>
<html>
<head>
</head>
<body>
<c:set var="cityName" value="Montreal"/>
<util:lookup cityName="${cityName}">
${cityName}'s province is ${province}.
${cityName}'s population is approximately ${population / 1000000} million.
</util:lookup>
</body>
</html>
</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 变量描述

JSP2.0自定义标签的更多相关文章

  1. Jsp2.0自定义标签(第一天)——一个简单的例子

    今天是学习自定义标签的第一天 Jsp2.0以来,自定义标签的实现比传统标签的实现容易了很多,一般只要extends类SimpleSupport重写doTag()方法即可. 先看最简单的例子,输出一个H ...

  2. Jsp2.0自定义标签(第二天)——自定义循环标签

    今天是学习自定义标签的第二天,主要是写一个自定义的循环标签. 先看效果图: 前台页面Jsp代码 <%@ page language="java" contentType=&q ...

  3. Jsp2.0自定义标签(第三天)——EL表达式的使用

    1.提出问题: 我们经常会看到这样的jsp页面代码: 浏览器显示: 为什么会在页面输出:Hello World  ,${per}究竟是如何找到“Hello World”的呢? 2.分析问题: 要想解决 ...

  4. JSP2的自定义标签和方法

    Jsp2的自定义标签 Jsp2 开发标签库的几个步骤: 开发自定义标签处理类. 建立一个*.tld文件,每个tld文件对应一个标签库,每个标签库可对应多个标签. 在jsp文件中使用自定义标签 空标签 ...

  5. JSP2 的自定义标签

    在 JSP 中开发标签库只需如下几个步骤 1.开发自定义标签处理类 2.建立一个 *.tld 文件,每个 *.tld 文件对应一个标签库,每个标签库可包含多个标签 3.在 JSP 文件中使用自定义标签 ...

  6. JSP2.2自定义标签、EL函数

    简介 JSTL是一个JSP标准标签库,可以解决大部分问题,但是如果我们需要一些更特殊的功能,就需要自定义类似JSTL中标签的标签.如果EL表达式无法满足我们的需求,我们也可以自定义EL函数. tld后 ...

  7. 开发JSP自定义标签

    互联网上有很多种自定义标签,今天学的这种非常简单哟 1 编写一个普通类在类中定义一个经常使用得到的 函数 如public String toUpper(String str){ ...... } 2 ...

  8. Servlet------>jsp自定义标签SimpleTag(jsp2.0以后的方法,1-5已经淘汰了)

    自定义标签能做什么: 1.移除java代码 2.控制jsp页面某一部分是否执行 3.控制整个jsp是否执行 3.jsp内容重复输出 4.修改jsp内容输出 位置: TagDemo1.java pack ...

  9. jsp2.0+中的标签文件,JSP Fragment技术

    刚进新公司不久,今天在看到项目中用到了.tag文件.刚开始我还以为这个是第三方类似freemarker的模板技术.问了下项目组的其他人员,原来这是jsp2.0以来就有的JSP Fragment技术.以 ...

随机推荐

  1. Java throw throws try...catch区别

    java里的异常多种多样,这是一种非常有用的机制,它能帮助我们处理那些我们未知的错误,在java里,关于异常的有throw throws,还有一个try catch 程序块.接下来我们挨个看看这几个的 ...

  2. CodeForces 803F Coprime Subsequences

    $dp$. 记$dp[i]$表示$gcd$为$i$的倍数的子序列的方案数.然后倒着推一遍减去倍数的方案数就可以得到想要的答案了. #include <iostream> #include ...

  3. jupyter notebook :一个交互式计算和开发环境

    一. IPython基础 代码自动补全:Tab键 可补全内容包括:变量名.函数名.成员变量函数.目录文件 内省(Itrospection) 在变量名之前或之后加上问号(?),这样可以显示这个对象的相关 ...

  4. css控制默认滚动条样式

    针对webkit内核的浏览器,使用伪类来改变滚动条的默认样式,详情如下: 滚动条组成部分 1. ::-webkit-scrollbar 滚动条整体部分 2. ::-webkit-scrollbar-t ...

  5. Typora ---一款简洁的Markdown编辑器

    Typora BB in front 如果你是一个佛(lan)系(duo),内心文艺的程序员,并且你对其他Markdown编辑器的使用效果感觉不是很好的话,可以来了解一下该软件Typora. What ...

  6. Visual Studio 2017为Android APK包签名

    Visual Studio 2017为Android APK包签名   为Android APK包签名,可以保证后期的App顺利升级.在Visual Studio 2015中,IDE会自动生成两个AP ...

  7. dev devfs udev sysfs及关系

        Linux 下对设备的管理方式主要有/dev和sysfs两种,前者是将设备注册为设备节点放入/dev目录下,而后者是在linux2.6内核后引入的新的文件系统. ➤/dev方式 关于/dev的 ...

  8. UVA11107 Life Forms --- 后缀数组

    UVA11107 Life Forms 题目描述: 求出出现在一半以上的字符串内的最长字符串. 数据范围: \(\sum len(string) <= 10^{5}\) 非常坑的题目. 思路非常 ...

  9. AC自动机详解(附加可持久化AC自动机)

    AC自动机 AC自动机,说白了就是在trie树上跑kmp(其实个人感觉比kmp容易理解).是一种多匹配串,单个主串的匹配.概括来说,就是将多个匹配串构造一个trie树,对于每个trie树的节点构造nx ...

  10. Codeforces Beta Round #10 C. Digital Root 数学

    C. Digital Root 题目连接: http://www.codeforces.com/contest/10/problem/C Description Not long ago Billy ...