j2ee自定义标签的最简单例子

总共需要四步

1 jsp  导入自定义标签

<%@ page language="java" import="java.util.*"  contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="WEB-INF/tlds/testlib.tld" prefix="yzj2" %>
 
<html>
    <head>    
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
        <title>自定义标签简单示例</title>
    </head>                     
    <body>
        <p>这里是正文内容</p>
        <yzj2:copyright/>
    </body>
</html>

2 web-info/目录下建立文件夹tags,tags下建立文件testlib.tld,testib.tld文件内容

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
 
<taglib>
    <tlibversion>1.0</tlibversion>
    <jspversion>1.1</jspversion>
    <shortname>yzj</shortname>
   
    <tag>
        <name>copyright</name>
        <tagclass>com.yanzhijun.CopyRightTag</tagclass>
        <bodycontent>empty</bodycontent>
        <attribute/>
    </tag>
</taglib>

3 写上面的配置文件需要的类

package com.yanzhijun;

import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
 
public class CopyRightTag extends TagSupport
{
    public int doEndTag()
    {
        try
        {  
            String copyPre = "版权所有2008";                        
            String info = new String(copyPre.getBytes(), "UTF-8");
            pageContext.getOut().println(copyPre);  
        }
        catch(IOException e){}
        return EVAL_PAGE;
    }   
}   

4 web.xml中进行引用,在tomcat启动的时候会加载标签的功能

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>tags</display-name>
  <jsp-config>
    <taglib>
      <taglib-uri>/testlib</taglib-uri>
      <taglib-location>/WEB-INF/tlds/testlib.tld</taglib-location>
    </taglib>
  </jsp-config>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值