Java custom tag

Source: Internet
Author: User
Tags time in milliseconds tld

1. Compile the **. tld file [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"> <! -- Tag library version --> <tlib-version> 1.0 </tlib-version> <! -- Default prefix of the tag library --> <short-name> candy </short-name> <! -- Default URI of the tag library --> <uri>/candy </uri> <! -- Webpage dialog box tag with mask --> <tag> <description> webpage dialog box tag with mask </description> <name> msgdialog </name> <tag-class> candy. tld. msgDialogTag </tag-class> <! -- The TAG body can be a static HTML element, expression language, however, the title text of the <body-content> scriptless </body-content> <attribute> <description> dialog box cannot appear, the default value is "tip" </description> <name> title </name> <required> false </required> <! -- JSP expressions can be used --> <rtexprvalue> true </rtexprvalue> </attribute> <description> specifies the mask height. The screen height is used by default, namely, 100% </description> <name> height </name> <required> false </required> <! -- You can use JSP expressions --> <rtexprvalue> true </rtexprvalue> </attribute> <description> the distance at the top of the dialog box, the default value is 300px </description> <name> top </name> <required> false </required> <! -- You can use a JSP expression --> <rtexprvalue> true </rtexprvalue> </attribute> <description> to specify the width of the dialog box, the default value is 500px </description> <name> boxwidth </name> <required> false </required> <! -- JSP expressions can be used --> <rtexprvalue> true </rtexprvalue> </attribute> <description> basic URL </description> <name> basepath </name> <required> true </required> <! -- JSP expressions can be used --> <rtexprvalue> true </rtexprvalue> </attribute> <description> temporary ID suffix to avoid ID conflict, the default value is the system time in milliseconds. </description> <name> tmpid </name> <required> false </required> <! -- JSP expressions can be used --> <rtexprvalue> true </rtexprvalue> </attribute> </tag> </taglib> 2. compile the tool class [java] package candy. tld; import java. io. *; import javax. servlet. jsp. jspException; import javax. servlet. jsp. tagext. jspFragment; import javax. servlet. jsp. tagext. simpleTagSupport;/** custom tag class in the dialog box with masks */public class MsgDialogTag extends SimpleTagSupport {String title = "tip "; // dialog box title text String height = "100% ";// The height of the mask. The default value is the screen height, that is, the distance from the top of the 100% String top = "300px"; // The default value is 100px String boxwidth = "330px "; // The dialog box width. The default value is 500px String basepath = ""; // basic URL String tmpid = null; // temporary ID suffix to avoid ID conflict, the default value is the system time in milliseconds/** TAG body Processing */public void doTag () throws JspException, IOException {// standard attribute value if (! Height. endsWith ("%") height = height + "px"; if (! Top. endsWith ("px") top = top + "px"; if (! Boxwidth. endsWith ("px") boxwidth = boxwidth + "px"; int titlewidth = Integer. valueOf (boxwidth. replaceAll ("px ","")). intValue ()-22; if (tmpid = null) tmpid = String. valueOf (System. currentTimeMillis (); // temporary ID suffix to avoid ID conflict // get the content of the existing TAG body JspFragment body = this. getJspBody (); StringWriter writer = new StringWriter (); body. invoke (writer); // The StringBuffer sb = new StringBuffer (); sb. append (" <Style> \ n "); sb. append ("# msgbox" + tmpid + "{width:" + boxwidth + "; background: # D6D3CE; border: 1px #848284 solid; padding: 0px; margin: 0 auto;} \ n "); sb. append (". msgicon {float: left; background-image: url ("+ basepath +"/img/msgtitle_icon.jpg); background-repeat: no-repeat; height: 20px; width: 20px ;} \ n "); sb. append (". msgtilte {float: left; text-align: left; background-image: url ("+ basepath +"/img/msgtitle_ B Ack.jpg); background-repeat: repeat-x; line-height: 20px; letter-spacing: 2px; height: 20px; width: "+ titlewidth +" px ;} \ n "); // sb. append (". msgclose {float: left; background-image: url ("// + basepath // +"/img/btn_close.jpg); background-repeat: no-repeat; height: 20px; width: 20px; cursor: pointer;} \ n "); sb. append (". msgmainbox {clear: both; border-top: 1px #848284 solid; text-align: left; padding: 20px; overflow: Uto;} \ n "); sb. append ("</style> \ n"); sb. append ("<div id = 'mask" + tmpid + "'style = 'clear: both; top: 0; left: 0; position: absolute; z-index: 10001; filter: alpha (opacity = 70); background: #000; opacity: 0.7;-moz-opacity: 0.7; height: "+ height +"; width: 100%; '> </div> \ n "); sb. append ("<div id = 'msgprompt" + tmpid + "'style = 'clear: both; top:" + top + "; left: 0; position: absolute; z-index: 10002; width: 100%; text-align: Center '> \ n "); sb. append ("<div id = 'msgbox" + tmpid + "'> \ n"); sb. append ("<div class = 'msgicon '> </div> \ n"); sb. append ("<div class = 'msgtilte '>" + title + "</div> \ n"); // sb. append ("<div class = 'msgclose' onClick = 'closemask" + tmpid // + "() '> </div> \ n"); sb. append ("<div class = 'msgmainbox'> \ n"); // Insert the prompt information sb in the TAG body. append (writer. toString (). trim () + "\ n"); sb. append ("</div> \ n"); sb. append ("</div> \ n"); sb. Append ("</div> \ n"); // sb. append ("<script language = 'javascript '> \ n"); // sb. append ("function closemask" + tmpid + "() {\ n"); // sb. append ("document. getElementById ('mask "+ tmpid // + "'). style. display = 'none'; \ n "); // sb. append ("document. getElementById ('msgprompt "+ tmpid // + "'). style. display = 'none'; \ n "); // sb. append ("} \ n"); // sb. append ("</script> \ n"); sb. append ("</div> \ n"); // output the processing result to getJsp on the page. Context (). getOut (). println (sb);} public String getTitle () {return title;} public void setTitle (String title) {this. title = title;} public String getBasepath () {return basepath;} public void setBasepath (String basepath) {this. basepath = basepath;} public String getHeight () {return height;} public void setHeight (String height) {this. height = height;} public String getTop () {return top;} Public void setTop (String top) {this. top = top;} public String getBoxwidth () {return boxwidth;} public void setBoxwidth (String boxwidth) {this. boxwidth = boxwidth;} public String getTmpid () {return tmpid;} public void setTmpid (String tmpid) {this. tmpid = tmpid;} 3. page import tag [html] <% @ taglib prefix = "candy" uri = "/candy" %> 4. page call Tag [html] <candy: msgdialog basepath = "<% = request. getContextPa Th () %> "> enter the logon password to unlock: <br> <input type = "text" id = "passText"> <input type = "button" onclick = "valSystemLock () "value =" unlock "class =" btn "style =" margin-bottom: 8px; "/> <br> <span id =" valmsg "style =" color: red; "> </span> </candy: msgdialog> in this example, the system is locked as an example to implement and use custom labels.

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.