日期:2014-05-16 浏览次数:20335 次
package org.test.function; import java.text.SimpleDateFormat; import java.util.Date; /** * 我的jstl函数 * @author afei * */ public class Function { public static String today(String pattern){ SimpleDateFormat sdf=new SimpleDateFormat(pattern); return sdf.format(new Date()); } }
<?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>JSTL 1.1 functions library by duyunfei</description> <display-name>JSTL functions</display-name> <tlib-version>1.1</tlib-version> <short-name>afei</short-name> <uri>http://duyunfei.iteye.com/</uri> <function> <description> get today by pattern </description> <name>today</name> <function-class>org.test.function.Function</function-class> <function-signature>java.lang.String today(java.lang.String)</function-signature> <example> Name: ${afei:today('yyyy-MM-dd')} </example> </function> </taglib>并把它部署到WEB-INF下,最后创建一个测试jsp,如
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib prefix="afei" uri="http://duyunfei.iteye.com/"%> <% String path = request.getContextPath(); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>测试一下jstl自定义函数</title> </head> <body> ${afei:today('yyyy-MM-dd')} ${afei:today('yyyy-MM-dd HH:mm:ss')} </body> </html>ok,成功了,非常简单的,但是很管用。