Heardytag. TLD
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <! Doctype taglib public "-// Sun Microsystems, Inc. // dtd jsp tag library 1.1 // "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> <br/> <taglib> <br/> <tlibversion> 1.0 </tlibversion> <br/> <jspversion> 1.1 </jspversion> <br/> <shortname> page </shortname> <br/> <tag> <br/> <Name> Hello </Name> <br/> <tagclass> heardy. mytag </tagclass> <br/> <bodycontent> JSP </bodycontent> </P> <p> <attribute> <br/> <Name> maxleng Th </Name> <br/> <required> false </required> <! -- Whether the attribute is optional --> <br/> <rtexprvalue> true </rtexprvalue> <! -- Whether to accept JSP expression computing results --> <br/> </attribute> <br/> </Tag> <br/> </taglib>
Mytag. Java
Package heardy; </P> <p> Import Java. io. ioexception; </P> <p> Import javax. servlet. JSP. jspexception; <br/> Import javax. servlet. JSP. jspwriter; <br/> Import javax. servlet. JSP. tagext. bodytagsupport; <br/> Import javax. servlet. JSP. tagext. tagsupport; </P> <p> public class mytag extends bodytagsupport {<br/>/** <br/> */<br/> Private Static final long serialversionuid = 167634601069545267l; <br/> private string maxlength = "10"; <br/> private string Info; </P> <p> Public String getmaxlength () {<br/> return maxlength; <br/>}</P> <p> Public void setmaxlength (string maxlength) {<br/> This. maxlength = maxlength; <br/>}</P> <p> Public String getinfo () {<br/> return Info; <br/>}</P> <p> Public void setinfo (string info) {<br/> this.info = Info; <br/>}</P> <p> @ override <br/> Public int doendtag () throws jspexception {<br/> jspwriter out = pagecontext. getout (); // The pagecontext object to be initialized by the container <br/> try {<br/> out. print (getinfo (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}< br/> return tagsupport. eval_page; // process other page content </P> <p >}</P> <p> @ override <br/> Public int dostarttag () throws jspexception {<br/> try {<br/> pagecontext. getout (). print (""); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}< br/> return tagsupport. eval_body_again; // The tag starts to process the content of the tag pair </P> <p >}</P> <p> @ override <br/> Public int doafterbody () throws jspexception {<br/> If (bodycontent. getstring (). length ()> integer. parseint (getmaxlength () <br/>. trim () <br/> setinfo ("<Div Title =/" "<br/> + bodycontent. getstring () <br/> + "/"> "<br/> + bodycontent. getstring (). substring (0, <br/> integer. parseint (getmaxlength (). trim () + "... </div> "); <br/> else {<br/> setinfo (" <div> "+ bodycontent. getstring () + "</div>"); <br/>}< br/> return tagsupport. skip_body; <br/>}</P> <p >}< br/>
Index. jsp
<% @ Page Language = "Java" pageencoding = "UTF-8" %> <br/> <% @ taglib uri = "/WEB-INF/heardytag. TLD "prefix =" heardy "%> <br/> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <br/> <HTML> <br/> <pead> <br/> <title> my JSP 'index' starting page </title> <br/> <meta http-equiv = "Pragma" content = "no-Cache"> <br/> <meta http- equiv = "cache-control" content = "no-Cache"> <br/> <meta http-equiv = "expires" content = "0"> <br/> <Meta HTTP-equiv = "keywords" content = "keyword1, keyword2, keyword3 "> <br/> <meta http-equiv =" Description "content =" this is my page "> <br/> </pead> <br/> <body> <br/> <peardy: hello maxlength = "5"> after </peardy: Hello> <br/> <peardy: Hello maxlength = "10"> afterafterafterafterafter </peardy: hello> <br/> <peardy: Hello maxlength = "5"> This is a long clip. </peardy: Hello> <br/> <peardy: hello maxlength = "5"> AF </peardy: Hello> <br/> </body> <br/> </ptml> <br/>
You can choose not to specify the maxlength. The default value is 10.