
已解决问题
谷歌zhaowenjun1224用户在2016.04.18提交了关于“中国矿业大学jsp 自定义标签实例”的提问,欢迎大家涌跃发表自己的观点。目前共有1个回答,最后更新于2025-03-02T21:48:23。希望大家能够帮助她。详细问题描述及疑问:期待您的答案,真心佩服你,谢谢 !
详细问题描述及疑问:期待您的答案,真心佩服你,谢谢 !
一、在WEB-I
复制代码代码如
<?xmlversion=
<!DOCTYPEtaglibPUBLIC"-//SunMicrosystems,Inc.//DTDJSPTag
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspv
<shortname>n
<tag>
<name>formatTime
<tagc
<bodycontent>empty</
<attribute>
<name>timestam
</att
<attribute>
<name>f
</
<attribute>
<na
</attribute>
<attribute>
<name>style</name>
</attribute>
</
</tagl
二、建一个c
复制代码代码如下:
packagecom.ufinity.taglibTest;
importj**a.io.IOException;
i
importj**a.text.ParseExc
i
importj**a
importj**a.***.Dat
importj**a
importj**ax.
/**
*Descriptionoftheclass
*
*@authorWangqy
*@version1.0
*@since2009-8-25
*/
publicclassFormatTimeTagextendsTagSupport{
/**
*serialVersionUIDlong
*/
privatestaticfinallongserialVersionUID=8757501937718830491L;
privateStringtimestamp;
privateStringformat;
privateStringshowTh**;
privateStringstyle;
publicintdoEndTag()
{
try
{
Stringinfo=this.convertDateTime(timestamp,format,Boolean.parseBoolean(showTh**),style);
pageContext.getOut().println(info);
}
catch(IOExceptione){
}
returnEVAL_PAGE;
}
privateStringconvertDateTime(StringdateTime,Stringformater,booleanshowTh**,StringcaseStyle){
StringtimePosted=**ll;
SimpleDateFormatdateFm=**ll;
DateFormatformat=newSimpleDateFormat("yyyyMMddh**h**mmss");
DateformatTime=**ll;
try{
formatTime=format.parse(dateTime);
}catch(ParseExceptione){
return**ll;
}
Calendarcalendar=Calendar.getInstance();
calendar.setTime(formatTime);
if(showTh**){
intday=calendar.get(***.DAY_OF_MONTh**);
StringdaySuffix="th";
if((day%10)==1){
daySuffix=((day/10)==1)?"th":"st";
}elseif((day%10)==2){
daySuffix=(day==12)?"th":"nd";
}elseif((day%10)==3){
daySuffix=(day==13)?"th":"rd";
}
formater=formater.substring(0,formater.indexOf(""))+"'"
+daySuffix+"'"
+formater.substring(formater.indexOf(""));
dateFm=newSimpleDateFormat(formater,Locale.ENGLISh**);
}else{
dateFm=newSimpleDateFormat(formater,Locale.ENGLISh**);
}
timePosted=dateFm.format(formatTime);
if(caseStyle.equals("Upper")){
timePosted=timePosted.toUpperCase();
}elseif(caseStyle.equals("Lower")){
timePosted=timePosted.toLowerCase();
}
returntimePosted;
}
publicvoidsetFormat(Stringformat){
this.format=format;
}
publicvoidsetShowTh**(StringshowTh**){
this.showTh**=showTh**;
}
publicvoidsetStyle(Stringstyle){
***.style=style;
}
publicvoidsetTimestamp(Stringtimestamp){
this.timestamp=timestamp;
}
}
三、建个jsp页面测试下:
复制代码代码如下:
<%@pagelanguage="j**a"pageEncoding="utf-8"%>
<%@tagliburi="WEB-INF/tlds/formatTime.tld"prefix="tf"%>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>自定义标签示例</title>
</head>
<body>
<p>
转换“20090403132233”
</p>
format:ddMMMMyyyyshowTh**:truestyle:Upper转换后为:<tf:formatTimeAsStringtimestamp="20090403132233"format="ddMMMMyyyy"showTh**="true"style="Upper"/><br/>
format:ddMMMMyyyyshowTh**:truestyle:Lower转换后为:<tf:formatTimeAsStringtimestamp="20090403132233"format="ddMMMMyyyy"showTh**="true"style="Lower"/><br/>
format:ddMMMMyyyyshowTh**:falsestyle:Upper转换后为:<tf:formatTimeAsStringtimestamp="20090403132233"format="ddMMMMyyyy"showTh**="false"style="Upper"/><br/>
format:ddMMMMyyyyshowTh**:truestyle:""转换后为:<tf:formatTimeAsStringtimestamp="20090403132233"format="ddMMMMyyyy"showTh**="true"style=""/><br/>
</body>
</html>
ok了。这里timestamp是一个给定值,如果是通过${}标签动态取的话,需要将tld文件的
复制代码代码如下:
<attribute>
<name>timestamp</name>
</attribute>
修改为
复制代码代码如下:
<attribute>
<name>timestamp</name>
<rtexprvalue>true</rtexprvalue>
</attribute>