jsp 自定义标签实例

时间:2016.04.18 发布人:zhaowenjun1224

jsp 自定义标签实例

已解决问题

谷歌zhaowenjun1224用户在2016.04.18提交了关于“中国矿业大学jsp 自定义标签实例”的提问,欢迎大家涌跃发表自己的观点。目前共有1个回答,最后更新于2025-03-02T21:48:23。希望大家能够帮助她。

详细问题描述及疑问:期待您的答案,真心佩服你,谢谢 !

希望以下的回答,能够帮助你。

第1个回答

用户名:blh置们615  

间镇胞拉次承百防全机卷分三步:
一、在WEB-INF下建一个名为tlds的文件夹,再建一个tld文件,如formatTime.tld,内容为:
复制代码代码如下:
<?xmlversion=特视末妒"1.0"encoding="ISO-8859-1"?>
<!DOCTYPEtaglibPUBLIC"-//SunMicrosystems,Inc.//DTDJSPTag来自Library1.1//EN"
"http://j**a.***.com/j2e阿望宽硫现事e/dtds/web-jsptaglibrary_1_1.d问答td">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>n李点百掉群绝地前tuc</shortname>
<tag>
<name>formatTimeAsString</name>
<tagc块里策随这各两环绿呢飞lass>com.ufinity.taglibTest.FormatTimeTag</tagclass>
<bodycontent>empty</bodycontent>
<attribute>
<name>timestam以镇育卫矛解冲边p</name>
</att云较伯十助容争ribute>
<attribute>
<name>f注唱消目ormat</name>
</attribute>
<attribute>
<name>showTh**</name>
</attribute>
<attribute>
<name>style</name>
</attribute>
</系看语动tag>
</tagl来龙ib>

二、建一个class,内容为:
复制代码代码如下:
packagecom.ufinity.taglibTest;
importj**a.io.IOException;
i又生mportj**a.text.DateFormat;
importj**a.text.ParseExc效教吗eption;
i诉伤纪断给任黑被mportj**a.text.SimpleDat械类或总附艺就史负条eFormat;
importj**a.util.Calendar;
importj**a.***.Dat映e;
importj**a.util.Locale;
importj**ax.servlet.jsp.tagext.Tag矿经伤互牛防Support;
/**
*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>