JSP custom tag

Source: Internet
Author: User
Tags dateformat tld

JSP page tag definition:

Implementation Behavior
The first step to implement tag behavior is to move the scriptlet code from its original location to a Java class (lastmodifiedtag), as shown in Listing 1:

Listing 1. Create a timestamp tag
Package com. jrcz. Aura. util;

Import java. Io. file;
Import java. Io. ioexception;
Import java. Text. dateformat;
Import java. util. date;

Import javax. servlet. http. httpservletrequest;
Import javax. servlet. jsp. tagext. tagsupport;
/*
* When writing the implementation class corresponding to the label, you need to reload the methods of the bodytagsupport class: dostarttag (), setbodycontent (),
* Doinitbody (), doafterbody (), doendtag ();
* Their execution sequence is as follows: dostarttag () → doinitbody () → setbodycontent () → doafterbody () → doendtag ()
* The dostarttag () method can return eval_body_include or skip_body. If eval_body_include is returned, the execution continues;
* If skip_body is returned, the following doinitbody (), setbodycontent (), and doafterbody () methods will not be executed,
* Directly execute the doendtag () method.
* The setbodycontent () method is used to set the content of the TAG body. If Initialization is required before this, it is completed in the doinitbody () method.
* After the label body content is executed, the doafterbody () method is called. This method returns eval_body_tag, skip_body, eval_page, or skip_page.
* If eval_body_tag is returned, the TAG body is set again until skip_body is returned;
* If eval_page is returned, the next part of the JSP page will be executed after the label body is executed;
* If skip_page is returned, the subsequent content of the JSP page will not be executed.
* Static constants in a Tag:
Eval_body_include: tells the server body content and sends the content to the output stream.
Skip_body: tells the server not to process the body content
Eval_page: allows the server to continue executing the page
Skip_page: prevents the server from processing the remaining page
Eval_body_again: Let the server continue to process the body content. Only the doafterbody method can return
Eval_body_buffered: Field of the bodytag interface, which is returned in dostarttag ().
Eval_body_include and skip_body are generally returned by dostarttag (), while eval_papge and skip_page are returned by doendtag.
*/
Public class lastmodifiedtag extends tagsupport {
Public int doendtag (){
Try {
Httpservletrequest request = (httpservletrequest) pagecontext. getrequest ();
String Path = pagecontext. getservletcontext (). getrealpath (request. getservletpath ());
File file = new file (PATH );
Dateformat formatter = dateformat. getdateinstance (dateformat. Long );
Pagecontext. getout (). println (formatter. Format (new date (file. lastmodified ())));
Pagecontext. getout (). println ("hello ");
System. Out. println ("Path:" + path );
} Catch (ioexception e ){
E. printstacktrace ();
}
Return eval_page;
}

}

The code in this method looks familiar. In essence, it is the same timestamp code we used earlier. Because user input is not required and the tag does not have attributes or embedded content, we only need to consider a new method.

Method is doendtag (). In this method, the tag can output content (in this example, the last modified data) to the JSP page.
Other changes in Listing 1 are more related to the Code marked as a JSP, but not to the scriptlet running on a page. For example, all JSP tags should extend the JSP class.

Javax. servlet. jsp. tagext. tagsupport. This class provides a basic framework for JSP tag. You may also notice that the returned eval_page. eval_page is a predefined integer constant that instructs the container to process the page.

. Another option is to use skip_page, which will stop processing the remaining part of the page. If you want to transfer the control to another page, for example, if you want to forward (forward) or redirect (redirect) users

Skip_page is required. The remaining details are related to the timestamp itself.
Next, compile this class, and put the lastmodifiedtag. Class file in a WEB-INF/classes directory, be sure to put it in the correct path hierarchy. This path should match the marked package name, the dot (.) in the package name (.)

Use a slash (/) instead. In this example, the directory path is the base path (WEB-INF/classes) plus the hierarchical COM/newinstance/site/tags. If there is a tag named Foo. Bar. Tag. mytag, It will be placed in

WEB-INF/classes/Foo/BAR/Tag. This path hierarchy ensures that the Web Container can find this class whenever it needs to load the tag.
Back to Top
Create TLD
The next step is to create a tag library Descriptor (TLD) file. TLD describes your tag Library to the container and any JSP page that uses the tag library. Listing 2 shows a very standard TLD, where

Only one tag is included. When you add more tags to the library, the length and complexity of the TLD file will increase.

Listing 2. A tag library Descriptor
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype taglib
Public "-// Sun Microsystems, Inc. // dtd jsp tag library 1.2/EN"
Http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd>
<Taglib>
<Tlib-version> 1.0 </tlib-version>
<JSP-version> 1.2 </JSP-version>
<Short-Name> GPB </short-Name>
<URI> http://www.newInstance.com/taglibs/site-utils </uri>
<Tag>
<Name> lastmodified </Name>
<Tag-class> com. jrcz. Aura. util. lastmodifiedtag </Tag-class>
<Body-content> Empty </body-content>
</Tag>
</Taglib>

The information at the top of the TLD file is applied to the entire tag library. In this example, I provide a version (which is useful for tracking the version of the JSP creator of a tag library, this is especially true when you need to modify the tag library frequently );

The JSP version on which the tag library depends, a prefix recommended for the tag library, and a URI used to reference the tag library. Note that I use the prefix short-name as part of the URI, so that it is easier to regard the prefix and the URI of the tag library

Overall.
The remaining information is used for a specific tag, which is represented by the Tag Element. I specify the name of the tag, the class used for the tag (this class should be compiled and placed in the appropriate place so that the container can load it), and finally specify

Mark whether embedded content exists. In this example, mark the content that is not embedded, so use "empty ".
Save the file and place it in the WEB-INF/TLDs directory (you may need to create this directory in your container ). I save this file as a site-utils.tld, And in this tag library the URI (recommended prefix) and TLD file itself

Create a clean link. For this specific tag library, The last step is to let your web application know how to connect to the URI in a JSP page and how to request to use a tag library. This can be achieved through the Application

. Listing 3 shows a very simple web. xml snippet that does this for our tag library.

Listing 3. link a URI with a tag Library
<Taglib>
<Taglib-Uri> http://www.newInstance.com/taglibs/site-utils </taglib-Uri>
<Taglib-location>/WEB-INF/TLDs/site-utils.tld </taglib-location>
</Taglib>

Back to Top
Packed
If you have followed these steps, you should be able to reference the new tag on the JSP page. Listing 4 shows us the newly improved footer. jsp. This file does not have a scriptlet or point

Reference the JSP page of The scriptlet.

Listing 4. Use the new tag Library
<% @ Taglib prefix = "Site-utils"
Uri = "http://www.newInstance.com/taglibs/site-utils" %>
</TD>
<TD width = "16" align = "Left" valign = "TOP"> </TD>
</Tr>
<! -- End main content -->
<Div id = "divab"> </div>
<Div> custom tag </div>
<GPB: lastmodified/>

After reading how jstl works in the previous sections (see "update your JSP page with jstl" and "import content to your web site, you should be clear about what to do next: We use web. the URI in the XML file

Use this tag library to assign it a prefix (the short-name from TLD is always the best choice) and then use this tag like any other JSP tag. The final result is a concise and better JSP page.

The JSP page is no longer running than a scriptlet.

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.