自定义标签

作用:把页面上常用/通用的功能改为标签形式来引用

第一步:在web-info下建立文件夹,分别管理自定义标签

第二步:在文件夹中建立.tld文件,写入通用的功能

tz.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>自定义标签</description>
	<display-name>自定一标签-函数形式</display-name>
	<tlib-version>1.0</tlib-version>
	<!-- 自定义标签的前缀 -->
	<short-name>tz</short-name>
	<uri></uri>
	<function>
		<description>日期的几秒钟前功能</description>
		<name>dataString</name>
		<function-class>com.tz.util.TmFunctions</function-class>
		<function-signature>java.lang.String dateToString2(java.util.Date)</function-signature>
		<example>${tz:dataString("2015-12-12 12:12:12")} 1分钟以前</example>
	</function>
	
	<function>
		<description>通过ip获取地址</description>
		<name>ipAddress</name>
		<function-class>com.tz.util.TmFunctions</function-class>
		<function-signature>java.lang.String ipAddress(java.lang.String)</function-signature>
		<example>${tz:ipAddress("127.0.0.1")} 本地</example>
	</function>
	
	<function>
		<description>格式化日期</description>
		<name>formateDate</name>
		<function-class>com.tz.util.TmFunctions</function-class>
		<function-signature>java.lang.String formateDate(java.util.Date,java.lang.String)</function-signature>
		<example>${tz:formateDate("2015-12-12 12:12:12",'yyyy')} 2015</example>
	</function>
	
	<function>
		<description>格式化流媒体的时长</description>
		<name>audiotime</name>
		<function-class>com.tz.util.TmFunctions</function-class>
		<function-signature>java.lang.String mpstime(int)</function-signature>
		<example>${tz:audiotime(183)} 03:04</example>
	</function>
</taglib>
第三步:定义一个类,实现功能
TmFunctions
package com.tz.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.tz.util.ip.TmIpUtil;


/**
 * 
 * JSP函数自定义标签
 * TmFunctions<BR>
 * 创建人:潭州学院-keke <BR>
 * 时间:2014年11月11日-下午9:21:37 <BR>
 * @version 1.0.0
 *
 */
public class TmFunctions {
	
	/**
	 * 日期具体的几秒钟以前
	 * 方法名:dateString<BR>
	 * @param startTime
	 * @return String<BR>
	 * @exception <BR>
	 * @since  1.0.0
	 */
	public static String dateToString(String startTime){
		return TmDateUtil.getTimeFormat(startTime);
	}
	
	/**
	 *  日期具体的几秒钟以前
	 * @exception <BR>
	 * @since  1.0.0
	 */
	public static String dateToString2(Date startTime){
		return TmDateUtil.getTimeFormat(startTime);
	}

	
	/**
	 * 通过ip地址获取真实地址
	 * @since  1.0.0
	 */
	public static String ipAddress(String ip){
		return TmIpUtil.ipLocation(ip);
	}
	
	/**
	 * 格式化日期的自定义函数
	 */
	public static String dateFormat(String dateString,String format){
		if(TmStringUtils.isEmpty(dateString))return "";
		try {
			Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateString);
			String ds =  new SimpleDateFormat(format).format(date);
			return ds;
		} catch (ParseException e) {
			return "";
		}
	}
	
	/**
	 * 
	 * 方法名:formateDate<BR>
	 * @exception <BR>
	 * @since  1.0.0
	 */
	public static String formateDate(Date date,String format){
		if(date==null)return "";
		String ds =  new SimpleDateFormat(format).format(date);
		return ds;
	}
	
	/**
	 * 根据视频时长获取分:秒
	 * @exception 
	 * @since  1.0.0
	 */
	public static String mpstime(int timeline){
		int minute = timeline / 60;
		int second = timeline % 60;
		return (minute<10?"0"+minute:minute+"")+":"+(second<10?"0"+second:second+"");
	}
}
TmIpUtil:

package com.tz.util.ip;

import javax.servlet.http.HttpServletRequest;

import com.tz.util.TmStringUtils;

public class TmIpUtil {

	/**
	 * 判断ip是否在指定网段中
	 * 
	 * @param iparea
	 * @param ip
	 * @return boolean
	 */
	public static boolean ipIsInNet(String iparea, String ip) {
		if (iparea == null)
			throw new NullPointerException("IP段不能为空!");
		if (ip == null)
			throw new NullPointerException("IP不能为空!");
		iparea = iparea.trim();
		ip = ip.trim();
		final String REGX_IP = "((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)";
		final String REGX_IPB = REGX_IP + "\\-" + REGX_IP;
		if (!iparea.matches(REGX_IPB) || !ip.matches(REGX_IP))
			return false;
		int idx = iparea.indexOf('-');
		String[] sips = iparea.substring(0, idx).split("\\.");
		String[] sipe = iparea.substring(idx + 1).split("\\.");
		String[] sipt = ip.split("\\.");
		long ips = 0L, ipe = 0L, ipt = 0L;
		for (int i = 0; i < 4; ++i) {
			ips = ips << 8 | Integer.parseInt(sips[i]);
			ipe = ipe << 8 | Integer.parseInt(sipe[i]);
			ipt = ipt << 8 | Integer.parseInt(sipt[i]);
		}
		if (ips > ipe) {
			long t = ips;
			ips = ipe;
			ipe = t;
		}
		return ips <= ipt && ipt <= ipe;
	}

	// IP判断
	public static boolean ipJudge(String ipArea, String ip) {
		boolean i = false;
		ipArea = TmStringSubUtil.LastSubStr(ipArea, ",");
		String[] arrayOfString = ipArea.split(",");
		for (int j = 0; j < arrayOfString.length; ++j)
			if (arrayOfString[j].indexOf("*") != -1) {
				if (!(ipJudgment(arrayOfString[j].replaceAll("\\*", "0") + "-"
						+ arrayOfString[j].replaceAll("\\*", "255"), ip)))
					continue;
				i = true;
				break;
			} else if (arrayOfString[j].indexOf(45) != -1) {
				if (!(ipJudgment(arrayOfString[j], ip)))
					continue;
				i = true;
				break;
			} else {
				if (!(arrayOfString[j].equals(ip)))
					continue;
				i = true;
				break;
			}
		return i;
	}

	public static boolean ipJudgment(String ipArea, String ip) {
		if (ipArea == null)
			throw new NullPointerException("IPBound is Null");
		if (ip == null)
			throw new NullPointerException("IP is Null");
		ipArea = ipArea.trim();
		ip = ip.trim();
		String str1 = "((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)";
		String str2 = "((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\-((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)";
		if ((!(ipArea
				.matches("((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\-((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)")))
				|| (!(ip.matches("((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)"))))
			return false;
		int i = ipArea.indexOf(45);
		String[] arrayOfString1 = ipArea.substring(0, i).split("\\.");
		String[] arrayOfString2 = ipArea.substring(i + 1).split("\\.");
		String[] arrayOfString3 = ip.split("\\.");
		long l1 = 0L;
		long l2 = 0L;
		long l3 = 0L;
		for (int j = 0; j < 4; ++j) {
			l1 = l1 << 8 | Integer.parseInt(arrayOfString1[j]);
			l2 = l2 << 8 | Integer.parseInt(arrayOfString2[j]);
			l3 = l3 << 8 | Integer.parseInt(arrayOfString3[j]);
		}
		if (l1 > l2) {
			long l4 = l1;
			l1 = l2;
			l2 = l4;
		}
		return ((l1 <= l3) && (l3 <= l2));
	}

	public static String ipFormat(String paramString) {
		return paramString.substring(0, paramString.lastIndexOf(".") + 1) + "*";
	}


	// 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址
	public static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("X-Forwarded-For");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		if (ip.equals("0:0:0:0:0:0:0:1"))
			return "127.0.0.1";
		return ip;
	}

	// 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址
	// public static String getIpAddr(HttpServletRequest request) {
	// String clientIp = request.getHeader("x-forwarded-for");
	// // log.info("All the IP address string is: " + strClientIp);
	// if (clientIp == null || clientIp.length() == 0 ||
	// "unknown".equalsIgnoreCase(clientIp)) {
	// clientIp = request.getRemoteAddr();
	// } else {
	// String[] ips = {};
	// // BusiAcceptAction.SplitsString(strClientIp, ',' , ipList); //
	// // 拆分字符串,可直接用String.plit方法
	// ips = clientIp.split(",");
	// String ip = new String();
	// for (int index = 0; index < ips.length; index++) {
	// ip = (String) ips[index];
	// if (!("unknown".equalsIgnoreCase(ip))) {
	// clientIp = ip;
	// break;
	// }
	// }
	// }
	// return clientIp;
	// }

	public static String ipLocation(String ip) {
		String ipLocation = "";
		TmIPSeeker ipSeeker = new TmIPSeeker("qqwry.dat","E:/JavaProjects02/tzmusic2/WebRoot/WEB-INF/ip/");
		ipLocation = ipSeeker.getIPLocation(ip).getCountry() + " "+ ipSeeker.getIPLocation(ip).getArea();
		return ipLocation;
	}
	
	public static String ipprovince(String ip) {
		String ipLocation = "";
		TmIPSeeker ipSeeker = new TmIPSeeker("qqwry.dat","D:/开发项目2/TM/TM/WebRoot/WEB-INF/plugin/ip/");
		ipLocation = ipSeeker.getIPLocation(ip).getCountry() ;
		return ipLocation;
	}

	public static String ipcity(String ip) {
		String ipLocation = "";
		TmIPSeeker ipSeeker = new TmIPSeeker("qqwry.dat","D:/开发项目2/TM/TM/WebRoot/WEB-INF/plugin/ip/");
		ipLocation =  ipSeeker.getIPLocation(ip).getArea();
		return ipLocation;
	}
	
	public static String ipLocation(String ip, String path) {
		String ipLocation = "";
		TmIPSeeker ipSeeker = new TmIPSeeker("qqwry.dat", path);
		ipLocation = ipSeeker.getIPLocation(ip).getCountry() + " "
				+ ipSeeker.getIPLocation(ip).getArea();
		return ipLocation;
	}

	/**
	 * 
	 * 方法名:ipLocation
	 * @param request
	 * @return String
	 * @exception 
	 * @since  1.0.0
	 */
	public static String ipLocation(HttpServletRequest request) {
		if(request==null)return "";
		try{
			String ipLocation = "";
			String ip = getIpAddr(request);
			String path = request.getRealPath("/") + "WEB-INF/ip";
			if (TmStringUtils.isNotEmpty(path)) {
				path = TmStringUtils.conversionSpecialCharacters(path);
				TmIPSeeker ipSeeker = new TmIPSeeker("qqwry.dat", path);
				ipLocation = ipSeeker.getIPLocation(ip).getCountry() + " "
						+ ipSeeker.getIPLocation(ip).getArea();
			}
			return ipLocation;
		}catch (Exception e) {
			return "";
		}
	}

	public static void main(String[] args) {
//		System.out.println(ipJudge("192.168.1.1", "192.168.1.1"));
//		System.out.println(ipJudge("192.168.1.*", "192.168.1.255"));
//		System.out.println(ipJudge("192.168.1.1-192.168.1.2", "192.168.1.1"));
//		System.out.println(ipJudge("202.168.1.1-202.168.1.3", "202.168.1.2"));
//		System.out.println(ipJudge("127.0.0.0-127.0.0.10", "127.0.0.0"));
//
//		System.out.println(ipJudge("192.168.1.1", "192.168.1.1"));
//		System.out.println(ipLocation("221.209.44.46"));
//		System.out.println(ipLocation("202.101.108.207"));
		System.out.println(ipLocation("27.38.99.250"));
		System.out.println(ipLocation("183.128.166.9"));
		System.out.println(ipLocation("210.22.70.225"));
		System.out.println(ipLocation("192.168.1.38"));
	}

}

TmDateUtil

package com.tz.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期工具类
 * TmDateUtil<BR>

 * 时间:2014年11月11日-下午10:29:09 <BR>
 * @version 1.0.0
 *
 */
public class TmDateUtil {

	/**
	 * 日期转换

	 * 时间:2014年11月11日-下午10:28:41 <BR>
	 * @param time
	 * @return Date<BR>
	 * @exception <BR>
	 * @since  1.0.0
	 */
	public static Date dateToString(String time){
		Date startTime = null;
		try {
			 startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return startTime;
	}
	
	
	/**
	 * 
	 * 方法名:getTimeFormat<BR>

	 * @return String<BR>
	 * @exception <BR>
	 * @since  1.0.0
	 */
	public static String getTimeFormat(String startTime){
		return getTimeFormat(dateToString(startTime));
	}
	
	/**
	 * 获取日期几分钟前,几年前

	 * 时间:2014年11月11日-下午10:27:54 <BR>
	 * @param startTime
	 * @return String<BR>
	 * @exception <BR>
	 * @since  1.0.0
	 */
	public static String getTimeFormat(Date startTime){
		try{
			long startTimeMills = startTime.getTime();
			long endTimeMills = System.currentTimeMillis();
			long diff = (endTimeMills - startTimeMills)/1000;//秒
			long day_diff  = (long) Math.floor(diff/86400);//天
			StringBuffer buffer = new StringBuffer();
			if(day_diff<0){
				return "[error],时间越界...";
			}else{
				if(day_diff==0 && diff<60){
					if(diff==0)diff=1;
					buffer.append(diff+"秒前");
				}else if(day_diff==0 && diff<120){
					buffer.append("1 分钟前");
				}else if(day_diff==0 && diff<3600){
					buffer.append(Math.round(Math.floor(diff/60))+"分钟以前");
				}else if(day_diff==0 && diff<7200){
					buffer.append("1小时前");
				}else if(day_diff==0 && diff<86400){
					buffer.append(Math.round(Math.floor(diff/3600))+"小时前");
				}else if(day_diff==1){
					buffer.append("1天前");
				}else if(day_diff<7){
					buffer.append(day_diff+"天前");
				}else if(day_diff <30){
					buffer.append(Math.round(Math.ceil( day_diff / 7 )) + " 星期前");
				}else if(day_diff >=30 && day_diff<=179 ){
					buffer.append(Math.round(Math.ceil( day_diff / 30 )) + "月前");
				}else if(day_diff >=180 && day_diff<365){
					buffer.append("半年前");
				}else if(day_diff>=365){
					buffer.append(Math.round(Math.ceil( day_diff /30/12))+"年前");
				}
			}
			return buffer.toString();
		}catch(Exception ex){
			return "";
		}
	}
}

第四步:jsp引入自定义标签使用
<%@taglib uri="/WEB-INF/tlds/tz.tld" prefix="tz" %>

<div class="helper clearfix" title="${tz:formateDate(comment.createTime,'yyyy年MM月dd日')}">
				${tz:dataString(comment.createTime)}   来自:${tz:ipAddress(comment.ip)}  <a href="javascript:;"
					class="btn-reply btn-action-reply">回复</a>   <a
					class="btn-vote btn-action-vote" href="javascript:;"><span>赞</span></a>
			</div>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值