自定义tag标签的方法
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 | 变量描述 |
自定义tag标签的方法的更多相关文章
- 自定义tag标签-实现long类型转换成Date类型
数据库里存储的是bigint型的时间,entity实体中存放的是long类型的标签,现在想输出到jsp页面,由于使用的是jstl标签,而要显示的是可读的时间类型,找来找去有个 fmt:formatDa ...
- Jsp 自定义tag标签
1转自:https://blog.csdn.net/yusimiao/article/details/46835617 Jsp自定义tag标签 自定义tag标签的好处 程序员可以自定一些特定功能的标记 ...
- struts2 自定义tag标签
在项目中可能有很多相同的jsp页面表示功能,这时可以使用自定义的tag进行定义,渐少重复的工作量便于日后维护! 下面就基于struts2进行自定义标签的定义与实现: 首先:自定义类MyTag继承str ...
- jsp如何自定义tag的标签库?
虽然和上一次的使用自定义的tld标签简化jsp的繁琐操作的有点不同,但是目的也是一致的.自定义tag比较简单. 1.新建tag标签 在WEB-INF目录下新建一个tags的文件夹,是自定义tag标签的 ...
- Inno Setup技巧[界面]添加和自定义左下角标签
原文 http://blog.sina.com.cn/s/blog_5e3cc2f30100cc49.html 本文介绍添加和自定义“左下角标签”的方法. 界面预览: Setup技巧[界面]添加和自定 ...
- 6.1 如何在spring中自定义xml标签
dubbo自定义了很多xml标签,例如<dubbo:application>,那么这些自定义标签是怎么与spring结合起来的呢?我们先看一个简单的例子. 一 编写模型类 package ...
- (转) ThinkPHP模板自定义标签使用方法
这篇文章主要介绍了ThinkPHP模板自定义标签使用方法,需要的朋友可以参考下 转之--http://www.jb51.net/article/51584.htm 使用模板标签可以让网站前台开发 ...
- 如何自定义JSTL标签与SpringMVC 标签的属性中套JSTL标签报错的解决方法
如何自定义JSTL标签 1.创建一个类,从SimpleTagSupport继承 A) 通过继承可以获得当前JSP页面上的对象,如JspContext I) 实际上可以强转为PageContext II ...
- JSP自定义tag控件标签
JSP支持自定tag的方法,那就是直接讲JSP代码保存成*.tag或者*.tagx的标签定义文件.tag和tagx文件不仅支持经典jsp代码,各种标签模版代码,还支持xml样式的jsp指令代码. 按照 ...
随机推荐
- Luogu1155 NOIP2008 双栈排序 【二分图染色】【模拟】
Luogu1155 NOIP2008 双栈排序 题目描述 Tom最近在研究一个有趣的排序问题.如图所示,通过 2个栈 S1 和 S2 ,Tom希望借助以下 44 种操作实现将输入序列升序排序. 操作 ...
- 如何创建一个基于命令行工具的跨平台的 NuGet 工具包
命令行可是跨进程通信的一种非常方便的手段呢,只需启动一个进程传入一些参数即可完成一些很复杂的任务.NuGet 为我们提供了一种自动导入 .props 和 .targets 的方法,同时还是一个 .NE ...
- UICollectionView官方使用示例代码研究
注:这里是iOS6新特征汇总贴链接 iOS6新特征:参考资料和示例汇总 这个链接可以学习到UICollectionView的相关介绍:iOS6新特征:UICollectionView介绍 由于UICo ...
- String.format()格式化日期(2)
在以前的开发中,日期格式化一直使用的是SimpleDateFormat进行格式化.今天发现String.format也可以格式化.当 然,两种方式的优劣没有进行深入分析. 1. 日期格式化 (2018 ...
- 关于matlab中定点数overflow的处理办法
定点数overflow的处理有两种办法:1,saturate,也就是说如果超过定点的最大值就取最大值,例如最大值是6结果是8,那么就取6:2,wrap,就是循环,如下图所示
- Vim自动补全插件----YouCompleteMe安装与配置
Vim自动补全插件----YouCompleteMe安装与配置 使用Vim编写程序少不了使用自动补全插件,在Linux下有没有类似VS中的Visual Assist X这么方便快捷的补全插件呢?以前用 ...
- adobe reader DC 字体设置
adobe reader DC 字体设置 一直使用adobe reader阅读pdf文档,系统提醒我升级一个reader助手, 升级之后: 感觉字体颜色变浅,笔画也变细了,整体有些模糊不清. goog ...
- 设置Maven的Web工程启动名称
java application的web工程名称就是工程名称:但是maven则不同,他的默认的website名称是在maven的pom文件里面的artifactId节点配置的值:例如: <gro ...
- PHP远程连接mysql
http://blog.chinaunix.net/uid-23215128-id-2951624.html # mysql -urootmysql> use mysql; Database c ...
- pandas之DateFrame 数据过滤+遍历行+读写csv-txt-excel
# XLS转CSV df = pd.read_excel(r'列表.xls') df2 = pd.DataFrame()df2 = df2.append(list(df['列名']), ignore_ ...