JSP2.0自定义标签
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自定义标签的更多相关文章
- Jsp2.0自定义标签(第一天)——一个简单的例子
今天是学习自定义标签的第一天 Jsp2.0以来,自定义标签的实现比传统标签的实现容易了很多,一般只要extends类SimpleSupport重写doTag()方法即可. 先看最简单的例子,输出一个H ...
- Jsp2.0自定义标签(第二天)——自定义循环标签
今天是学习自定义标签的第二天,主要是写一个自定义的循环标签. 先看效果图: 前台页面Jsp代码 <%@ page language="java" contentType=&q ...
- Jsp2.0自定义标签(第三天)——EL表达式的使用
1.提出问题: 我们经常会看到这样的jsp页面代码: 浏览器显示: 为什么会在页面输出:Hello World ,${per}究竟是如何找到“Hello World”的呢? 2.分析问题: 要想解决 ...
- JSP2的自定义标签和方法
Jsp2的自定义标签 Jsp2 开发标签库的几个步骤: 开发自定义标签处理类. 建立一个*.tld文件,每个tld文件对应一个标签库,每个标签库可对应多个标签. 在jsp文件中使用自定义标签 空标签 ...
- JSP2 的自定义标签
在 JSP 中开发标签库只需如下几个步骤 1.开发自定义标签处理类 2.建立一个 *.tld 文件,每个 *.tld 文件对应一个标签库,每个标签库可包含多个标签 3.在 JSP 文件中使用自定义标签 ...
- JSP2.2自定义标签、EL函数
简介 JSTL是一个JSP标准标签库,可以解决大部分问题,但是如果我们需要一些更特殊的功能,就需要自定义类似JSTL中标签的标签.如果EL表达式无法满足我们的需求,我们也可以自定义EL函数. tld后 ...
- 开发JSP自定义标签
互联网上有很多种自定义标签,今天学的这种非常简单哟 1 编写一个普通类在类中定义一个经常使用得到的 函数 如public String toUpper(String str){ ...... } 2 ...
- Servlet------>jsp自定义标签SimpleTag(jsp2.0以后的方法,1-5已经淘汰了)
自定义标签能做什么: 1.移除java代码 2.控制jsp页面某一部分是否执行 3.控制整个jsp是否执行 3.jsp内容重复输出 4.修改jsp内容输出 位置: TagDemo1.java pack ...
- jsp2.0+中的标签文件,JSP Fragment技术
刚进新公司不久,今天在看到项目中用到了.tag文件.刚开始我还以为这个是第三方类似freemarker的模板技术.问了下项目组的其他人员,原来这是jsp2.0以来就有的JSP Fragment技术.以 ...
随机推荐
- Qt基础——让使用Designer创建的UI也能自动适应窗口大小
原文请看:http://www.cnblogs.com/linmeng/archive/2012/07/05/2559259.html 我们知道,通过Qt的各种Layout可以实现控件的自动布局. 但 ...
- Spring JDBC主从数据库访问配置
通过昨天学习的自定义配置注释的知识,探索了解一下web主从数据库的配置: 背景:主从数据库:主要是数据上的读写分离: 数据库的读写分离的好处? 1. 将读操作和写操作分离到不同的数据库上,避免主服务器 ...
- 选项卡栏控制器(UITabBarController)
选项卡栏控制器管理的每个场景都包含一个UITabBarItem,它包含标题.图像和徽章. 在场景里可以通过tabBarItem属性来获得UITabBarItem的引用.例如:[self.tabBarI ...
- hdu 4819 二维线段树模板
/* HDU 4819 Mosaic 题意:查询某个矩形内的最大最小值, 修改矩形内某点的值为该矩形(Mi+MA)/2; 二维线段树模板: 区间最值,单点更新. */ #include<bits ...
- 【BZOJ 3997】 3997: [TJOI2015]组合数学 (DP| 最小链覆盖=最大点独立集)
3997: [TJOI2015]组合数学 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 919 Solved: 664 Description 给出 ...
- 「BZOJ 4502」串
「BZOJ 4502」串 题目描述 兔子们在玩字符串的游戏.首先,它们拿出了一个字符串集合 \(S\),然后它们定义一个字符串为"好"的,当且仅当它可以被分成非空的两段,其中每一段 ...
- NOI1999 JZYZOJ1289 棋盘分割 dp 方差的数学结论
http://172.20.6.3/Problem_Show.asp?id=1289 除了下标一坨一坨屎一样挺恶心其他都还挺容易的dp,这道题才发现scanf保留小数位是四舍五入的,惊了. f[k][ ...
- Nginx 502 Bad Gateway 解决方法
proxy_next_upstream error timeout invalid_header http_500 http_503;或者尝试设置:large_client_header_buffer ...
- bzoj1977 次小生成树
Description 小 C 最近学了很多最小生成树的算法,Prim 算法.Kurskal 算法.消圈算法等等. 正当小 C 洋洋得意之时,小 P 又来泼小 C 冷水了.小 P 说,让小 C 求出一 ...
- C# 通过HttpWebRequest在后台对WebService进行调用
通过HttpWebRequest在后台对WebService进行调用 http://www.cnblogs.com/macroxu-1982/archive/2009/12/23/1630415.ht ...