无为清净楼资源网 Design By www.qnjia.com
分三步:
一、在WEB-INF 下建一个名为tlds的文件夹,再建一个tld文件,如 formatTime.tld,内容为:
复制代码 代码如下:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>ntuc</shortname>
<tag>
<name>formatTimeAsString</name>
<tagclass>com.ufinity.taglibTest.FormatTimeTag</tagclass>
<bodycontent>empty</bodycontent>
<attribute>
<name>timestamp</name>
</attribute>
<attribute>
<name>format</name>
</attribute>
<attribute>
<name>showTH</name>
</attribute>
<attribute>
<name>style</name>
</attribute>
</tag>
</taglib>
二、建一个class,内容为:
复制代码 代码如下:
package com.ufinity.taglibTest;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import javax.servlet.jsp.tagext.TagSupport;
/**
* Description of the class
*
* @author Wangqy
* @version 1.0
* @since 2009-8-25
*/
public class FormatTimeTag extends TagSupport {
/**
* serialVersionUID long
*/
private static final long serialVersionUID = 8757501937718830491L;
private String timestamp;
private String format;
private String showTH;
private String style;
public int doEndTag()
{
try
{
String info = this.convertDateTime(timestamp, format, Boolean.parseBoolean(showTH), style);
pageContext.getOut().println(info);
}
catch (IOException e) {
}
return EVAL_PAGE;
}
private String convertDateTime(String dateTime, String formater, boolean showTH, String caseStyle) {
String timePosted = null;
SimpleDateFormat dateFm = null;
DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
Date formatTime = null;
try {
formatTime = format.parse(dateTime);
} catch (ParseException e) {
return null;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(formatTime);
if (showTH) {
int day = calendar.get(Calendar.DAY_OF_MONTH);
String daySuffix = "th";
if ((day % 10) == 1) {
daySuffix = ((day / 10) == 1) ? "th" : "st";
} else if ((day % 10) == 2) {
daySuffix = (day == 12) ? "th" : "nd";
} else if ((day % 10) == 3) {
daySuffix = (day == 13) ? "th" : "rd";
}
formater = formater.substring(0, formater.indexOf(" ")) + "'"
+ daySuffix + "'"
+ formater.substring(formater.indexOf(" "));
dateFm = new SimpleDateFormat(formater, Locale.ENGLISH);
} else {
dateFm = new SimpleDateFormat(formater, Locale.ENGLISH);
}
timePosted = dateFm.format(formatTime);
if (caseStyle.equals("Upper")) {
timePosted = timePosted.toUpperCase();
} else if (caseStyle.equals("Lower")) {
timePosted = timePosted.toLowerCase();
}
return timePosted;
}
public void setFormat(String format) {
this.format = format;
}
public void setShowTH(String showTH) {
this.showTH = showTH;
}
public void setStyle(String style) {
this.style = style;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
}
三、建个jsp页面测试下:
复制代码 代码如下:
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="WEB-INF/tlds/formatTime.tld" prefix="tf" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>自定义标签示例</title>
</head>
<body>
<p>
转换“20090403132233”
</p>
format:dd MMMM yyyy showTH:true style:Upper 转换后为:<tf:formatTimeAsString timestamp="20090403132233" format="dd MMMM yyyy" showTH="true" style="Upper"/><br/>
format:dd MMMM yyyy showTH:true style:Lower 转换后为:<tf:formatTimeAsString timestamp="20090403132233" format="dd MMMM yyyy" showTH="true" style="Lower"/><br/>
format:dd MMMM yyyy showTH:false style:Upper 转换后为:<tf:formatTimeAsString timestamp="20090403132233" format="dd MMMM yyyy" showTH="false" style="Upper"/><br/>
format:dd MMMM yyyy showTH:true style:"" 转换后为:<tf:formatTimeAsString timestamp="20090403132233" format="dd MMMM yyyy" showTH="true" style=""/><br/>
</body>
</html>
ok了。这里timestamp是一个给定值,如果是通过${}标签动态取的话,需要将tld文件的
复制代码 代码如下:
<attribute>
<name>timestamp</name>
</attribute>
修改为
复制代码 代码如下:
<attribute>
<name>timestamp</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
一、在WEB-INF 下建一个名为tlds的文件夹,再建一个tld文件,如 formatTime.tld,内容为:
复制代码 代码如下:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>ntuc</shortname>
<tag>
<name>formatTimeAsString</name>
<tagclass>com.ufinity.taglibTest.FormatTimeTag</tagclass>
<bodycontent>empty</bodycontent>
<attribute>
<name>timestamp</name>
</attribute>
<attribute>
<name>format</name>
</attribute>
<attribute>
<name>showTH</name>
</attribute>
<attribute>
<name>style</name>
</attribute>
</tag>
</taglib>
二、建一个class,内容为:
复制代码 代码如下:
package com.ufinity.taglibTest;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import javax.servlet.jsp.tagext.TagSupport;
/**
* Description of the class
*
* @author Wangqy
* @version 1.0
* @since 2009-8-25
*/
public class FormatTimeTag extends TagSupport {
/**
* serialVersionUID long
*/
private static final long serialVersionUID = 8757501937718830491L;
private String timestamp;
private String format;
private String showTH;
private String style;
public int doEndTag()
{
try
{
String info = this.convertDateTime(timestamp, format, Boolean.parseBoolean(showTH), style);
pageContext.getOut().println(info);
}
catch (IOException e) {
}
return EVAL_PAGE;
}
private String convertDateTime(String dateTime, String formater, boolean showTH, String caseStyle) {
String timePosted = null;
SimpleDateFormat dateFm = null;
DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
Date formatTime = null;
try {
formatTime = format.parse(dateTime);
} catch (ParseException e) {
return null;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(formatTime);
if (showTH) {
int day = calendar.get(Calendar.DAY_OF_MONTH);
String daySuffix = "th";
if ((day % 10) == 1) {
daySuffix = ((day / 10) == 1) ? "th" : "st";
} else if ((day % 10) == 2) {
daySuffix = (day == 12) ? "th" : "nd";
} else if ((day % 10) == 3) {
daySuffix = (day == 13) ? "th" : "rd";
}
formater = formater.substring(0, formater.indexOf(" ")) + "'"
+ daySuffix + "'"
+ formater.substring(formater.indexOf(" "));
dateFm = new SimpleDateFormat(formater, Locale.ENGLISH);
} else {
dateFm = new SimpleDateFormat(formater, Locale.ENGLISH);
}
timePosted = dateFm.format(formatTime);
if (caseStyle.equals("Upper")) {
timePosted = timePosted.toUpperCase();
} else if (caseStyle.equals("Lower")) {
timePosted = timePosted.toLowerCase();
}
return timePosted;
}
public void setFormat(String format) {
this.format = format;
}
public void setShowTH(String showTH) {
this.showTH = showTH;
}
public void setStyle(String style) {
this.style = style;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
}
三、建个jsp页面测试下:
复制代码 代码如下:
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="WEB-INF/tlds/formatTime.tld" prefix="tf" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>自定义标签示例</title>
</head>
<body>
<p>
转换“20090403132233”
</p>
format:dd MMMM yyyy showTH:true style:Upper 转换后为:<tf:formatTimeAsString timestamp="20090403132233" format="dd MMMM yyyy" showTH="true" style="Upper"/><br/>
format:dd MMMM yyyy showTH:true style:Lower 转换后为:<tf:formatTimeAsString timestamp="20090403132233" format="dd MMMM yyyy" showTH="true" style="Lower"/><br/>
format:dd MMMM yyyy showTH:false style:Upper 转换后为:<tf:formatTimeAsString timestamp="20090403132233" format="dd MMMM yyyy" showTH="false" style="Upper"/><br/>
format:dd MMMM yyyy showTH:true style:"" 转换后为:<tf:formatTimeAsString timestamp="20090403132233" format="dd MMMM yyyy" showTH="true" style=""/><br/>
</body>
</html>
ok了。这里timestamp是一个给定值,如果是通过${}标签动态取的话,需要将tld文件的
复制代码 代码如下:
<attribute>
<name>timestamp</name>
</attribute>
修改为
复制代码 代码如下:
<attribute>
<name>timestamp</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
标签:
jsp,自定义标签
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年11月15日
2024年11月15日
- 老头杯第二届规则是什么 英雄联盟老头杯第二届规则介绍
- 王崴-爵士听堂.蓝色波萨(HQCD)[WAV+CUE]
- 群星《欧美动听情歌·柔情第5季》2CD【DTS-WAV分轨】
- [极品珍藏]德意志进行曲集卡拉扬SACD[WAV+CUE]
- 前暴雪制作人呼吁反击DEI 玩家:夺回文化!
- 腾讯证实子公司Sharkmob大规模裁员:整个市场很低迷
- 荣耀加冕 问鼎冠军 中国代表队获第四届东亚电竞锦标赛团体总冠军
- 污甩乐队.2024-Let.the.dirt.left【摩登天空】【FLAC分轨】
- 杜德伟.1994-ALL.FOR.YOU(英)【滚石】【WAV+CUE】
- 群星.2013-百代好声音ADMSCD1【EMI百代】【WAV+CUE】
- 群星《私人音乐精选示范碟》PRIVATEMUSIC 发烧唱片名碟[WAV+CUE][1.1G]
- 山口百惠《あなたへの子守唄》日本索尼钢刻字首版[WAV分轨][1.1G]
- 群星《宝丽金20周年特别发烧版》1:1母盘直刻限量编号[低速原抓WAV+CUE][1G]
- 凤飞飞.1984-仲夏(夏艳)[WAV]
- 常安《民歌红·江南燕》DTS-ES6.1[WAV]