欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jsp 自定义标签实例

程序员文章站 2023-11-26 19:51:28
分三步: 一、在web-inf 下建一个名为tlds的文件夹,再建一个tld文件,如 formattime.tld,内容为: 复制代码 代码如下:
分三步:
一、在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>