zoukankan      html  css  js  c++  java
  • JSP 自定义标签的开发流程

    1.编写一个实现Tag接口的标签处理器类

     1 package cn.itcast.web.tag;
     2 
     3 import java.io.IOException;
     4 
     5 import javax.servlet.http.HttpServletRequest;
     6 import javax.servlet.jsp.JspException;
     7 import javax.servlet.jsp.JspWriter;
     8 import javax.servlet.jsp.PageContext;
     9 import javax.servlet.jsp.tagext.Tag;
    10 
    11 public class ViewIPTag implements Tag {
    12 
    13     private PageContext pageContext;
    14 
    15     public int doEndTag() throws JspException {
    16         // TODO Auto-generated method stub
    17         return 0;
    18     }
    19 
    20     public int doStartTag() throws JspException {
    21         // TODO Auto-generated method stub
    22 
    23         HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
    24         JspWriter out = pageContext.getOut();
    25 
    26         String ip = request.getRemoteAddr();
    27         try {
    28             out.write(ip);
    29         } catch (IOException e) {
    30             // TODO Auto-generated catch block
    31 
    32             throw new RuntimeException(e);
    33         }
    34 
    35         return 0;
    36     }
    37 
    38     public Tag getParent() {
    39         // TODO Auto-generated method stub
    40         return null;
    41     }
    42 
    43     public void release() {
    44         // TODO Auto-generated method stub
    45 
    46     }
    47 
    48     public void setPageContext(PageContext arg0) {
    49         // TODO Auto-generated method stub
    50 
    51         this.pageContext = arg0;
    52     }
    53 
    54     public void setParent(Tag arg0) {
    55         // TODO Auto-generated method stub
    56 
    57     }
    58 
    59 }

    2.在/WEB-INF/目录下新建tld文件,在tld文件中对标签处理器进行描述

    tld文件中的3种标签体类型

    empty

    JSP

    scriptless

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 
     3 <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
     4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     5     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
     6     version="2.0">
     7     <description>A tag library exercising SimpleTag handlers.</description>
     8     <tlib-version>1.0</tlib-version>
     9     <short-name>SimpleTagLibrary</short-name>
    10     <uri>/itcast</uri>
    11 
    12     <tag>
    13         <name>viewIP</name>
    14         <tag-class>cn.itcast.web.tag.ViewIPTag</tag-class>
    15         <body-content>empty</body-content>
    16     </tag>
    17     
    18 </taglib>

    3.在JSP页面中导入并使用自定义标签

     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2 <%@ taglib uri="/itcast" prefix="itcast"%>
     3 
     4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     5 <html>
     6 <head>
     7 <title>My JSP '1.jsp' starting page</title>
     8 </head>
     9 
    10 <body>
    11 
    12     你的IP地址是:
    13     <itcast:viewIP />
    14 
    15 </body>
    16 </html>
  • 相关阅读:
    C++ Primer Plus(三)
    C++ Primer Plus(二)
    C++ Primer Plus(一)
    C Primer Plus(三)
    C++ 函数重载,函数模板和函数模板重载,选择哪一个?
    Spring IoC 公共注解详解
    Spring IoC @Autowired 注解详解
    Spring IoC 容器的扩展
    Spring IoC bean 的初始化
    Spring IoC 属性赋值阶段
  • 原文地址:https://www.cnblogs.com/denggelin/p/8159182.html
Copyright © 2011-2022 走看看