自定义jsp标签入门
2014-03-29 19:51
309 查看
java代码
tld文件
jsp页面
http://java.itheima.com/jsp/core</uri><tag> <description> show client ip </description> <name>showIp</name> <tag-class>com.itcast.zdyJsp.ShowIPTag</tag-class> <body-content>empty</body-content> </tag></taglib>
package com.itcast.zdyJsp; import java.io.IOException; import javax.management.RuntimeErrorException; import javax.servlet.http.HttpServletRequest; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; public class ShowIPTag extends TagSupport { /** * */ private static final long serialVersionUID = 1L; /** * 开始标签 * 1.自定义标签的作用:消除jsp中的java脚本 * 二:编写步骤: * 1.编写一个类 实现 Tag 接口 (一般继承 TagSupport) * 覆盖掉对应的方法: * * 注:可以通过pageContext对象获取其他8打对象的引用 * 3.在WEB-INF 中扩展名为tld的xml文件 */ @Override public int doStartTag() throws JspException { HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); String ip = request.getRemoteAddr(); JspWriter out = pageContext.getOut(); try { out.print(ip); } catch (IOException e) { throw new RuntimeException(e); } return super.doStartTag(); } }
tld文件
<?xml version="1.0" encoding="UTF-8"?> <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>HEIMA1.0 core library</description> <display-name>HEIMA core</display-name> <tlib-version>1.0</tlib-version> <short-name>itheima</short-name> <uri> http://java.itheima.com/jsp/core</uri> <tag> <description> show client ip </description> <name>showIp</name> <tag-class>com.itcast.zdyJsp.ShowIPTag</tag-class> <body-content>empty</body-content> </tag> </taglib>
jsp页面
<?xml version="1.0" encoding="UTF-8"?> <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>HEIMA1.0 core library</description> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.itheima.com/jsp/core" prefix="itheima" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'zdyJspdemo.jsp' starting page</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> This is my JSP page. <br> <itheima:showIp/> </body> </html>
http://java.itheima.com/jsp/core</uri><tag> <description> show client ip </description> <name>showIp</name> <tag-class>com.itcast.zdyJsp.ShowIPTag</tag-class> <body-content>empty</body-content> </tag></taglib>
相关文章推荐
- JSP自定义标签开发入门
- JavaWeb学习总结(二十三)——jsp自定义标签开发入门
- JSP自定义简单标签入门之带有属性
- JSP自定义标签开发入门
- JSP自定义标签开发入门
- JavaWeb总结二十三、jsp自定义标签开发入门
- JSP自定义标签开发入门
- JSP自定义标签开发入门实例
- JSP自定义标签开发入门
- JavaWeb开发之十七:jsp自定义标签开发入门
- JSP自定义标签开发入门
- JSP自定义标签开发入门
- 自定义JSP标签入门
- javaweb学习总结(二十三)——jsp自定义标签开发入门
- 深入分析JavaWeb Item23 -- jsp自定义标签开发入门
- JSP自定义标签开发入门
- JSP自定义标签开发入门
- JSP自定义标签开发入门