Custom Struts2 simple tag

Source: Internet
Author: User
Tags dateformat

Take the tag for obtaining the current time as an example to develop a custom tag bin: currentTime. The procedure is as follows:


1. First define a tag processing class.


[Java]
Package org. binbin. tag;
 
Import java. io. IOException;
Import java. text. DateFormat;
Import java. text. SimpleDateFormat;
Import java. util. Date;
 
Import javax. servlet. jsp. JspException;
Import javax. servlet. jsp. JspWriter;
Import javax. servlet. jsp. tagext. TagSupport;
 
/**
* Custom tags
* @ Author binbin (cn.binbin@qq.com)
* @ Date 2013-3-28
*/
Public class TimeTag extends TagSupport
{
Private static final long serialVersionUID = 6918846280074418825L;
 
@ Override
Public int doStartTag () throws JspException
{
JspWriter out = this. pageContext. getOut ();

// Define the time output format
DateFormat df = new SimpleDateFormat ("yyyy/MM/dd HH: mm: ss ");
// Obtain the specified format string for the current time
String str = df. format (new Date ());

Try
{
// Output the current time string to the page
Out. print (str );
} Catch (IOException e)
{
// If an exception occurs, the current program cannot handle it. Therefore, a runtime exception is thrown again.
Throw new RuntimeException (e );
}
Return super. doStartTag ();
}
}

Package org. binbin. tag;

Import java. io. IOException;
Import java. text. DateFormat;
Import java. text. SimpleDateFormat;
Import java. util. Date;

Import javax. servlet. jsp. JspException;
Import javax. servlet. jsp. JspWriter;
Import javax. servlet. jsp. tagext. TagSupport;

/**
* Custom tags
* @ Author binbin (cn.binbin@qq.com)
* @ Date 2013-3-28
*/
Public class TimeTag extends TagSupport
{
Private static final long serialVersionUID = 6918846280074418825L;

@ Override
Public int doStartTag () throws JspException
{
JspWriter out = this. pageContext. getOut ();

// Define the time output format
DateFormat df = new SimpleDateFormat ("yyyy/MM/dd HH: mm: ss ");
// Obtain the specified format string for the current time
String str = df. format (new Date ());

Try
{
// Output the current time string to the page
Out. print (str );
} Catch (IOException e)
{
// If an exception occurs, the current program cannot handle it. Therefore, a runtime exception is thrown again.
Throw new RuntimeException (e );
}
Return super. doStartTag ();
}
}
2. Then add a tag library definition file bin. tld under the WEB-INF directory


[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Taglib xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
Version = "2.0">
<Tlib-version> 1.0 </tlib-version>
<Short-name> bin </short-name>
<Uri>/bin-tags </uri>

<Tag>
<Name> currentTime </name>
<Tag-class> org. binbin. tag. TimeTag </tag-class>
<Body-content> empty </body-content>
</Tag>
</Taglib>

<? Xml version = "1.0" encoding = "UTF-8"?>
<Taglib xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
Version = "2.0">
<Tlib-version> 1.0 </tlib-version>
<Short-name> bin </short-name>
<Uri>/bin-tags </uri>
 
<Tag>
<Name> currentTime </name>
<Tag-class> org. binbin. tag. TimeTag </tag-class>
<Body-content> empty </body-content>
</Tag>
</Taglib>
3. Use the tag on the jsp page


[Html]
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<! -- Import the corresponding tag Library first -->
<% @ Taglib uri = "/bin-tags" prefix = "bin" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> My JSP '1. jsp 'starting page </title>
</Head>
<Body>
Current System Time: <bin: currentTime/>
</Body>
</Html>

<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<! -- Import the corresponding tag Library first -->
<% @ Taglib uri = "/bin-tags" prefix = "bin" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> My JSP '1. jsp 'starting page </title>
</Head>
<Body>
Current System Time: <bin: currentTime/>
</Body>
</Html>

Iv. Running Effect

 

 

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.