日期:2014-05-16 浏览次数:20543 次
标签开发之几大步骤:
1,开发标签类,继承TagSupport类,
package org.lxh.tagdemo; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; public class HelloTag extends TagSupport{ public int doStartTag()throws JspException{ JspWriter out =super.pageContext.getOut(); try{ out.println("<h1>hello World!!</h1>"); }catch(Exception e){} return TagSupport.SKIP_BODY; } };
注意点 :编译的时候可能报错,请注意 tomcat路径下,jsp-api.jar的路径问题,把此文件夹放到 java环境下,jdk/jre/lib/ext 包下面,
2,定义一个标签的描述文件,标签的描述文件的后缀 "*.tld",而且此文件也符合XML语法规则。
<?xml version="1.0" encoding="UTF-8"?> <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_1.xsd" version="2.1"> <tlib-version>1.0<tlib-version> <!--表示标签库的版本--> <short-name>firsttag</short-name> <!--为标签库在TLD中的描述名称--> <tag> <name>hello</name> <tag-class>org.lxh.tagdemo.HelloTag</tag-class> <body-content>empty</body-content> </tag> </taglib>
?3,在JSP文件之中使用此标签。
?
4,标签组成部分
?
?4.1>.标签处理类: HelloTag.java;
?4.2>.标签描述文件:hellotag.tlc
?4.3>.JSP页面: 通过<%@ taglib%>定义标签.
?4.4>(可选)在web.xml文件中配置映射名称.
?
********************************************************************************
1,定义有属性的标签
示例:自定义日期格式化操作
package org.lxh.tagdemo; import java.text.*; import java.util.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; public class DateTag extends TagSupport{ private String format; //设置属性的时候可以通过setter完成 public int doStartTag()throws JspException{ SimpleDateFormat sdf = new SimpleDateFormat(this.format); //表示进行格式化的日期显示操作 try{ super.pageContext.getOut().write(sdf.format(new Date())); }catch (Exception e){ e.printStackTrace(); } return TagSupport.SKIP_BODY; } public void setFormat(String format){ this.format = format; } public String getFormat(){ return this.format; } };?
2,编写 *.tld文件,
<?xml version="1.0" encoding="UTF-8"?> <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_1.xsd" version="2.1"> <tlib-version>1.0<tlib-version> <!--表示标签库的版本--> <short-name>datetag</short-name> <!--为标签库在TLD中的描述名称--> <tag> <name>date</name> <tag-class>org.lxh.tagdemo.DateTag</tag-class> <body-content>empty</body-content> <attribute> <!-- 定义属性 --> <name>format</name> <required>true</required> <rtexprvalue>true</rtexprvalue><!-- 支持表达式输出 --> </attribute> </tag> </taglib>?
3,为了方便的使用,直接在web.xml文件之中定义此标签库.
?
<?xml version="1.0" encoding="ISO-8859-1"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIO