|
有一次看到同事写的自定义标签,发现是如此的简单。
1,写个.tld放在classpath下,如下,
myselfxu.tld
<?xml version="1.0" encoding="ISO-8859-1" ?><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>Custom Functions</description> <tlib-version>1.0</tlib-version> <short-name>xu</short-name> <uri>/myfunction</uri> <function> <name>testMyself</name> <function-class>com.kuanglong.commons.MySelf</function-class> <function-signature>java.lang.String testMyself(java.lang.String)</function-signature> </function> <function> <name>encode</name> <function-class>java.net.URLEncoder</function-class> <function-signature>java.lang.String encode(java.lang.String,java.lang.String)</function-signature> </function></taglib>
2,java类的代码,
Myself.java
package com.kuanglong.commons;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class MySelf {public static String testMyself(String ss){return ss ;}final static int ISO_TIME = 0;final static int ISO_DATETIME = 1;final static int ISO_DATE = 2;final static int SLASH_DATETIME = 3;final static int SLASH_DATE = 4;final static int NO_SLASH_DATETIME = 5;final static int NO_SLASH_DATE = 6;final static int NO_SLASH_DATE_ExcludeYear = 8;final static int SIMPLE_DATE_ExcludeYear = 9;static SimpleDateFormat dateFormatDashSecond = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); static SimpleDateFormat dateFormatDashDay = new SimpleDateFormat("yyyy-MM-dd");static SimpleDateFormat dateFormatSlashSecond = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); static SimpleDateFormat dateFormatSlashDay = new SimpleDateFormat("yyyy/MM/dd"); static SimpleDateFormat dateFormatSecond = new SimpleDateFormat("yyyyMMdd HH:mm:ss"); static SimpleDateFormat dateFormatDay = new SimpleDateFormat("yyyyMMdd"); static SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm"); static SimpleDateFormat dateFormatDashDayExcludeYear = new SimpleDateFormat("MM-dd"); static SimpleDateFormat dateFormatDayExcludeYear = new SimpleDateFormat("MMdd"); public static String dateFormat(Object date,int type){if(date instanceof Date){if(type == ISO_TIME){return timeFormat.format(date);} else if(type == ISO_DATE){return dateFormatDashDay.format(date);} else if(type == SLASH_DATETIME){return dateFormatSlashSecond.format(date);} else if(type == SLASH_DATE){return dateFormatSlashDay.format(date);} else if(type == NO_SLASH_DATETIME){return dateFormatSecond.format(date);} else if(type == NO_SLASH_DATE){return dateFormatDay.format(date);}else if(type == NO_SLASH_DATE_ExcludeYear){return dateFormatDashDayExcludeYear.format(date);}else if(type == SIMPLE_DATE_ExcludeYear){return dateFormatDayExcludeYear.format(date);} else{//type == ISO_DATETIMEreturn dateFormatDashSecond.format(date);}} else if(date instanceof Calendar){return dateFormat(((Calendar)date).getTime(),type);} else return "";}}
3,jsp上引用自己定义的标签。。
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %><%@ taglib prefix="myfn" uri="/myfunction" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title></head> ${myfn:testMyself("时的方式方式来得及浮士德了家里开始就")}<body></body></html>
|
|