JSP2.0自定义标签
来源:互联网 发布:张成泽犬决 知乎 编辑:程序博客网 时间:2024/04/30 17:09
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>
- <%@ 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>
- <%@ 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>
*使用标签:
- <%@ 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>
- <?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使用的脚本语言,目前必须是javadisplay-name标签名small-iconfor toolslarge-iconfor toolsdescription标签作用描述exampleinformal description of how the tag is used<jsp:directive:attribute>的可选属性
name属性名requiredtrue or falsertexprvaluetrue or false - 指定是否支持运行时表达式type值类型 - 默认是java.lang.Stringfragmenttrue or false - 值先传递给容器(false), 直接传给标签处理方法(true)description属性描述
<jsp:directive:variable>的可选属性
name-given变量名(标签使用时的变量名)name-from-attributeSpecifies 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.aliasA locally scoped variable which will store the variable's value. Used only with name-from-attribute.variable-class变量类.默认是java.lang.String.declareIndicates 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变量描述
0 0
- JSP2.0自定义标签
- JSP2.0自定义标签
- JBuilder2005 下 jsp2.0 自定义标签
- JSP2.0 tags自定义标签学习笔记
- jsp2.0自定义标签(二)
- jsp2.0自定义标签(一)
- JSP2的自定义标签
- JSP2的自定义标签
- JSP2的自定义标签
- jsp2的自定义标签
- jsp2自定义标签
- jsp2的自定义标签
- JSP2-自定义标签
- JSP2的自定义标签详解
- 一,JSP2的自定义标签
- JSP2自定义标签库技术
- JSP2 Taglib 自定义标签学习
- 关于jsp2.0中简单的自定义标签
- MyBatis学习(5)---------调用存储过程和缓存
- 如何防止搜索引擎收录抓取页面内容
- JNI笔记
- 获取系统相册
- UIButton
- JSP2.0自定义标签
- 关于人性,我是这么看的——“唯进化”论!
- 使用 CSS 媒体查询创建响应式网站
- uva208递归枚举
- 【Hibernate】(3)Hibernate单表操作
- 标准ASCII编码
- Searching Quickly
- ubuntu 视频转换 ffmpeg mencoder
- MySQL怎么计算打开文件数