JSP Custom Label Instance _jsp programming

Source: Internet
Author: User
Tags dateformat locale string format tld
Three-step:
First, under the Web-inf to build a folder named TLDs, and then build a TLD file, such as FORMATTIME.TLD, the content is:
Copy Code code as follows:

<?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>

Second, the construction of a class, the content is:
Copy Code code as follows:

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;
}
}

Third, the establishment of a JSP page test:
Copy Code code as follows:

<%@ page language= "java" pageencoding= "Utf-8"%>
<%@ taglib uri= "Web-inf/tlds/formattime.tld" prefix= "TF"%>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Custom Label Example </title>
<body>
<p>
Convert "20090403132233"
</p>
FORMAT:DD MMMM yyyy showth:true Style:upper converted to: <tf:formattimeasstring timestamp= "20090403132233" format= "DD MMMM yyyy "showth=" true "style=" Upper "/><br/>
FORMAT:DD MMMM yyyy showth:true Style:lower converted to: <tf:formattimeasstring timestamp= "20090403132233" format= "DD MMMM yyyy "showth=" true "style=" Lower "/><br/>
FORMAT:DD MMMM yyyy showth:false Style:upper converted to: <tf:formattimeasstring timestamp= "20090403132233" format= "DD MMMM yyyy "Showth=" false "style=" Upper "/><br/>
FORMAT:DD MMMM yyyy showth:true style: "" After conversion: <tf:formattimeasstring timestamp= "20090403132233" format= "dd MMMM yyyy "Showth=" true "style=" "/><br/>
</body>

OK, okay. Here timestamp is a given value and if it is dynamically fetched through the ${} tag, you need to add the TLD file's
Copy Code code as follows:

<attribute>
<name>timestamp</name>
</attribute>

Amended to
Copy Code code as follows:

<attribute>
<name>timestamp</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.