Use JSP custom tags

Source: Internet
Author: User
Tags tld
JSP custom tags are actually quite useful. For example, you can insert a few simple sentences on a complicated page.

To avoid a large amount of redundant code. For example, you can create a custom tag for playing a magic lamp on a news website. The following is a general example of custom tags:

Public class slidenewstag extends simpletagsupport {

/** TAG body Processing */

Public void dotag () throws jspexception, ioexception {

.......

// Use the webapplicationcontextutils tool class to obtain the DaO instance in the Spring IoC container

Dao = (basedaoimpl) webapplicationcontextutils. getrequiredwebapplicationcontext (pagecontext) getjspcontext (). getservletcontext (). getbean ("Dao ");

Stringbuffer sb = new stringbuffer ();

List list = Dao. Query (hql, 1, number );

If (list = NULL | list. Size () = 0 ){

// Output the processing result to the page

Getjspcontext (). getout (). println ("");

Return;

}

Iterator it = List. iterator ();

SB. append ("<script language = JavaScript> \ n ");

SB. append ("Var focus_width" + slideno + "=" + width + ";/* slide news Image Width */\ n ");

SB. append ("Var focus_height" + slideno + "=" + height + ";/* slide news Image Height */\ n ");

SB. append ("Var text_height" + slideno + "= 20;/* slide News Text title height */\ n ");

SB. append ("Var swf_height" + slideno + "= focus_height" + slideno + "+ text_height" + slideno + "; \ n ");

SB. append ("Var pics" + slideno + "=''; \ n ");

SB. append ("Var links" + slideno + "=''; \ n ");

SB. append ("Var texts" + slideno + "=''; \ n ");

SB. append ("function ATI" + slideno + "(URL, IMG, title) \ n ");

SB. append ("{\ n ");

SB. append ("If (pics" + slideno + "! = '') \ N ");

SB. append ("{\ n ");

SB. append ("pics" + slideno + "= \" | \ "+ pics" + slideno + "; \ n ");

SB. append ("Links" + slideno + "= \" | \ "+ Links" + slideno + "; \ n ");

SB. append ("texts" + slideno + "= \" | \ "+ texts" + slideno + "; \ n ");

SB. append ("}");

SB. append ("pics" + slideno + "= escape (IMG) + pics" + slideno + "; \ n ");

SB. append ("Links" + slideno + "= escape (URL) + Links" + slideno + "; \ n ");

SB. append ("texts" + slideno + "= title + texts" + slideno + "; \ n ");

SB. append ("} \ n ");

SB. append ("</SCRIPT> \ n ");

SB. append ("<script language = JavaScript> \ n ");

While (it. hasnext ()){

OBJ = (News) it. Next ();

If (obj. gettitle (). Length ()> titlelen ){

SB. append ("ATI" + slideno + "('" + baseurl + obj. gethtmlpath () + "','" + baseurl + "/" + obj. getpicture (). trim () + "','" + tools. cutstring (obj. gettitle (), titlelen * 2) + "'); \ n ");

} Else {

SB. append ("ATI" + slideno + "('" + baseurl + obj. gethtmlpath () + "','" + baseurl + "/" + obj. getpicture (). trim () + "','" + obj. gettitle () + "'); \ n ");

}

}

SB. append ("document. write ('<embed src = \ "" + baseurl + "/JS/pixviewer.swf \" wmode = \ "Opaque \" flashvars = \ "pics =' + pics" + slideno + "+ '& links =' + Links" + slideno + "+ '& texts =' + texts" + slideno + "+ '& borderwidth =' + focus_width" + slideno +" + '& borderheight =' + focus_height "+ slideno +" + '& textheight =' + text_height "+ slideno +" + '\ "menu = \" false \ "bgcolor = \ "# dadada \" Quality = \ "High \" width = \ "'+ focus_width" + slideno + "+' \" Height = \ "'+ swf_height" + slideno +" + '\ "allowScriptAccess = \" samedomain \ "type = \" application/X-Shockwave-Flash \ "/> '); \ n ");

SB. append ("</SCRIPT> \ n ");

// Output the processing result to the page

Getjspcontext (). getout (). println (SB );

}

As you can see, it is actually a constant assembly of HTML, nothing complicated and special.

2. Create a XXX. TLD File

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

<! -- Tag library version -->

<Tlib-version> 1.0 </tlib-version>

<! -- Default prefix of the tag library -->

<Short-Name> eportal </short-Name>

<! -- Default URI of the tag library -->

<URI>/eportal </uri>

<Tag>

<Description> slide News list tag </description>

<Name> slidenews </Name>

<Tag-class> com. eportal. TLD. slidenewstag </Tag-class>

<! -- The TAG body is empty -->

<Body-content> Empty </body-content>

<Attribute>

<Description> topic number. Multiple topic numbers are separated by numbers, for example, I _001 and I _002 </description>

<Name> section </Name>

<Required> true </required>

<Rtexprvalue> true </rtexprvalue>

</Attribute>

<Attribute>

<Description> News Type: 0-Latest news 1-headlines 2-hot news 3-excellent recommendations 4-all types </description>

<Name> newstype </Name>

<Required> false </required>

<Rtexprvalue> true </rtexprvalue>

</Attribute>

<Attribute>

<Description> Number of news entries </description>

<Name> Number </Name>

<Required> true </required>

<Rtexprvalue> true </rtexprvalue>

</Attribute>

<Attribute>

<Description> slide width </description>

<Name> width </Name>

<Required> true </required>

<Rtexprvalue> true </rtexprvalue>

</Attribute>

<Attribute>

<Description> slide height </description>

<Name> height </Name>

<Required> true </required>

<Rtexprvalue> true </rtexprvalue>

</Attribute>

<Attribute>

<Description> title words </description>

<Name> titlelen </Name>

<Required> true </required>

<Rtexprvalue> true </rtexprvalue>

</Attribute>

<Attribute>

<Description> basic URL </description>

<Name> baseurl </Name>

<Required> false </required>

<Rtexprvalue> true </rtexprvalue>

</Attribute>

<Attribute>

<Description> serial number of the current slide on this page </description>

<Name> slideno </Name>

<Required> true </required>

<Rtexprvalue> true </rtexprvalue>

</Attribute>

</Tag>

Note that all <attribute> must set getter and Setter in Java.

3 call

<% @ Taglib prefix = "E" uri = "/eportal" %>

<! -- Use a custom magic lamp News Tag -->

<E: slidenews section = "001" number = "5" titlelen = "30" width = "440" Height = "260" baseurl = "<% = basepath %>" slideno = "1 "/>

Do not place the TLD under Web-INF.

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.