欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

自定义jstl标签

程序员文章站 2022-06-08 18:09:36
...

1.新建一个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>A tag library exercising SimpleTag handlers.</description>  
    <tlib-version>1.0</tlib-version>  
    <short-name>SimpleTagLibrary</short-name>  
    <uri>/test</uri>  <!--为该标签配一个uri-->  
      
    <tag>  
    <name>viewIP</name> <!-- 为标签处理器类配一个标签名 --> 
    <tag-class>demo.spring.mvc.common.ViewIPTag</tag-class>  
    <body-content>empty</body-content>  
    </tag>  
      
</taglib>  

其中 uri 对应jsp中引用的<%@ taglib uri="/test" >

tag-class 对应的是具体实体类的内容,具体这个标签的内容是在后台生成的。

2.新建a.jsp,引入刚才声明的taglib,内容如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <jsp:include page="../com/com.jsp"></jsp:include>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <%@ taglib uri="/test" prefix="testIp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<c:set var="ct" value="${pageContext.request.contextPath}"></c:set>
<script src="../js/jquery.min.js"></script> 
<script src="${ct}/js/aa.js" type="text/javascript"></script> 
<script src="${ctx}/js/bb.js" type="text/javascript"></script> 
<!-- <script type="text/javascript">
	alert(222);
</script> -->
</head>
<body>
you and me!
<div style="position: relative;"> 11111111</div>
 <div style="position: absolute;">您的IP是:<testIp:viewIP/></div>
</body>
</html>

3.具体的ViewIPTag类实现的 Tag 接口,复写其中的doTag方法,内容如下:

package demo.spring.mvc.common;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class ViewIPTag implements Tag{

	private PageContext pageContext;  
	@Override
	public int doEndTag() throws JspException {
	   HttpServletRequest request2 = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
       HttpServletResponse response = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
       String ip = request2.getRemoteAddr(); //通过request获取客户机的ip  
        try {
			PrintWriter out2=response.getWriter();
			out2.write("<div style='color:red' position:relation>"+ip+"</div>");
		} catch (IOException e1) {
			e1.printStackTrace();
		}
        return 0;
	}

	@Override
	public int doStartTag() throws JspException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Tag getParent() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void release() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setPageContext(PageContext arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setParent(Tag arg0) {
		// TODO Auto-generated method stub
		
	}

	
}


相关标签: 自定义jstl标签