zoukankan      html  css  js  c++  java
  • jsp自定义标签

      1.java类实现tag接口,实现了tag接口,这个类叫做标签处理器,我们可以继承Tagsupport,这个类实现了Tag接口

      2.编写tld文件,放在webinf下,头和尾在tomcat的目录下去抄:apache-tomcat-6.0.35webappsexamplesWEB-INFjsp2jsp2-example-taglib.tld

    简单的写一个

    1.标签处理器

     1 public class ViewIPtag extends TagSupport {
     2 
     3     @Override
     4     public int doStartTag() throws JspException {
     5         
     6         /*
     7           在创建时,已经为我们传入了一个pageContext,管理所有域的对象 
     8          */
     9         JspWriter out = this.pageContext.getOut();
    10         
    11         HttpServletRequest request =  (HttpServletRequest) this.pageContext.getRequest();
    12         String ip = request.getRemoteAddr();
    13         try {
    14             out.print(ip);
    15         } catch (IOException e) {
    16             throw new RuntimeException(e);
    17         }
    18         
    19         return super.doStartTag();
    20     }
    21     
    22 }    

    2.tld

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
     4     version="2.0">
     5     <!-- 描述,可以删去-->
     6     <description>A tag library exercising SimpleTag handlers.</description>
     7     <!-- 版本号-->
     8     <tlib-version>1.0</tlib-version>
     9     <short-name>dwl</short-name>
    10     <!-- uri,这个是标签的地址-->
    11     <uri>http://www.dwl.com</uri>
    12     <tag>
    13         <!-- 描述,可以删去-->
    14         <description>Outputs Hello, World</description>
    15         <name>viewIP</name>
    16         <!-- 标签处理器的路径-->
    17         <tag-class>com.du.tag.ViewIPtag</tag-class>
    18         <!-- 默认,有没有标签体,empty没有-->
    19         <body-content>empty</body-content>
    20     </tag>
    21 </taglib>

    3.jsp页面引入

     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2 <%@ taglib uri="http://www.dwl.com" prefix="dwl"  %>
     3 
     4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     5 <html>
     6   <head>   
     7    <title>自定义标签</title>    
     8   </head>
     9   
    10   <body>
    11     您的ip是:<dwl:viewIP/>
    12   </body>
    13 </html>
    如果有使用请标明来源:http://www.cnblogs.com/duwenlei/
  • 相关阅读:
    最精简的django程序
    spring+mongo
    从零搭建mongo分片集群的简洁方法
    java对redis的基本操作
    awk输出指定列
    sed输出指定行
    Bash的循环结构(for和while)
    用ffmpeg切割音频文件
    Python判断字符串是否全是字母或数字
    Python函数: any()和all()的用法
  • 原文地址:https://www.cnblogs.com/duwenlei/p/3503261.html
Copyright © 2011-2022 走看看