jsp 自定义标签【继承TagSupport类】【区别与例一】 简单例子二

来源:互联网 发布:手机软件连不上网络 编辑:程序博客网 时间:2024/05/01 21:57

标签库组成:

1。标签处理程序类----实现标签所代表功能的程序段

2。标签库描述文件(tld)---描述标签处理程序与前端标签的匹配

3。标签指示----用在jsp显示端的

要实现一个简单的,计算,字符串长度的标签。

(一):标签处理程序:

注意:这里可以,实现Tag接口,或者,重写它的支持类TagSupport

package taglib;

import java.io.IOException;
import java.util.Date;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;
/**
 *
 * @author zwc
 *
 */
@SuppressWarnings("serial")
public class TimeUtils extends TagSupport {
 private Logger logger = Logger.getLogger(TimeUtils.class);
 @Override
 public int doEndTag() throws JspException {
  try {
   JspWriter out = pageContext.getOut();
   out.write("taglib end time: " + new Date());
  } catch (IOException e) {
   logger.warn("jsp out wrong!!", e);
  }
  return TagSupport.EVAL_BODY_INCLUDE;
 }

 @Override
 public int doStartTag() throws JspException {
  try {
   JspWriter out = pageContext.getOut();
   out.write("taglib start time: " + new Date());
  } catch (IOException e) {
   logger.warn("jsp out wrong!!", e);
  }
  return TagSupport.EVAL_PAGE;
 }
 
}

注意:这里,重写了,标签开始,标签结束的两个方法

(二)标签库,描述文件tld

<?xml version="1.0" encoding="UTF-8" ?>
<!--
  Copyright 2004 The Apache Software Foundation
  Licensed 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 CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<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_0.xsd"
  version="2.0">
 
  <description>A tag library exercising function handlers.</description>注意:该属性是可选择,属性,为了对该标签库的描述
  <tlib-version>1.0</tlib-version>注意:这里是必选值,否则,调用出错。
  <short-name>test</short-name>注意,该属性是可选择属性,因为在jsp页面定义了前缀,这里在定义一个,简略名,不知是什么意思。
  <uri>/test-1.0</uri>注意:这里定义的路径,表示,jsp找到该标签的唯一路径
  
  <tag>
   <name>getTime</name>
   <tag-class>taglib.TimeUtils</tag-class>
  </tag>

 


</taglib>

(三)前端jsp,应用

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="test" uri="/test-1.0"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>Error</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>
  <body>
    Error<br>
    <jsp:useBean id="user" class="bean.User"/>
 <jsp:setProperty property="name" value="abc" name="user"/>
    <jsp:getProperty property="name" name="user"/>
   
    Test: ${test:getStringLength("yuanfen860913")}
   
    TestA:<test:getTime/>
  </body>
</html>

 

 

 

(四) web.xml中声明该标签,可以,声明,也可以不用,声明,因为,默认的,会去找/WEB-INF  下的,所有tld,标签库描述文件,然后自动加载

 <jsp-config>
  <taglib>
   <taglib-uri>/test-1.0</taglib-uri>
   <taglib-location>test-1.0.tld</taglib-location>
  </taglib>
  
 </jsp-config>

 

(五)运行结果:

 

 Error
abc Test: 13

TestA:taglib start time: Wed Jun 30 11:48:27 CST 2010taglib end time: Wed Jun 30 11:48:27 CST 2010