码迷,mamicode.com
首页 > Web开发 > 详细

JSP 自定义标签

时间:2017-12-31 23:26:28      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:int   let   rar   style   encoding   pac   catch   etag   地址   

 

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文件中对标签处理器进行描述

 

 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>

 

JSP 自定义标签

标签:int   let   rar   style   encoding   pac   catch   etag   地址   

原文地址:https://www.cnblogs.com/denggelin/p/8159182.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!