JSP custom tag test Original article: Rabbit brother 3: JSP custom tag Test
I. Overview
JSPThere is an important technology: Custom tags (Custom tagIn the last few days.StrutsFoundStrutsUsing a lot of custom tags, suchHtml,Bean. So I made a simple experiment and learned this technology.
Let's first introduce this technology!
1 . Advantages:
ReplacedJSPInJavaProgramAnd can be used repeatedly to make it easy to get familiarJavaProgramming web page designer.
2 . Development Process:
(1)WriteJSP, InJSP.
(2)InWeb. xmlSpecified inJSPLabel used in. TLD (Tag library description file)File Location.
(3). TLDThe class used by the label specified in the file.
3. Classification of custom tags:
(1)Simple labels: such<Mytag:Helloworld/>
(2)With attribute tags: for example<Imytag:Checkinput dbname = "<mybean. getdbname ()>"/>
(3)Tags with tags:
The part between the start and end tags of a custom tag is the TAG body.(Body).BodyThe content can beJSPCan also beHtml, Script language, or other custom labels.
<Mytag:Checkinput dbname = "<mybean. getdbname ()>">
<Mytag: Log message = "table name">
<Mytag:Checkinput/>
(4)Can beS BranchLabels used:
DefinedIDAndTypeAttribute labels can beS criptlet.
<Mytag:Connection id = "oradb" type = "datasource" name = "oracle">
<% Oradb. getconnection (); %>
4 . Interface and others
In fact, the processing class of custom tags is implementedTag HandlerObject.JSPTechnology inJavax. servlet. jsp. tagextMultipleTag HandlerInterface,Jsp1.2Defined inTag,Bodytag,IterationtagInterface, inJsp2.0AddedSimpletagInterface.JSPAlso provides the implementation class of the above interfaceTagsupport,BodytagsupportAndSimpletagsupport(SimpletagsupportOnlyJsp2.0).BodytagsupportImplementedBodytag,TagAndIterationtagInterface.
Interface and Method
TagInterface |
Method |
Simpletag |
Dotage |
Tag |
Dostarttag, doendtag, release |
Iterationtag |
Dostarttag, doaftertag, release |
Bodytag |
Dostarttag, doendtag, release, doinitbody, doafterbody |
The following table is derived fromSunOfJSPOnline tutorial.
Tag handler Methods |
Tag handler type |
Methods |
Simple |
Dostarttag, doendtag, release
|
Attributes |
Dostarttag, doendtag, set/getattribute1. .. n, release
|
Body, evaluation and no interaction |
Dostarttag, doendtag, release
|
Body, iterative Evaluation |
Dostarttag, doafterbody, doendtag, release
|
Body, Interaction |
Dostarttag, doendtag, release, doinitbody, doafterbody, release
|
In the following tableEvalYesEvaluateAbbreviation, meaning: Comment,Estimation,Please...In the following return values: execution.
Return Value |
Meaning |
Skip_body |
Indicates that you do not need to process the TAG body and call it directly.Doendtag ()Method. |
Skip_page |
IgnoreJSPContent. |
Eval_page |
Process the tag.JSP. |
Eval_body_buffered |
Indicates the TAG body to be processed. |
Eval_body_include |
Indicates the TAG body to be processed,But BypassSetbodycontent ()AndDoinitbody ()Method |
Eval_body_again |
Loop processing of the label body. |
For more information, see references.
SunOfJavaTutorials:Java.sun.com/webservices/docs/1.0/tutorial/doc/jsptags.html "> http://java.sun.com/webservices/docs/1.0/tutorial/doc/JSPTags.html
Ii. Experiment 1 . Test Introduction
The following experiment is developed based on the above development process.
(1) InJSPSpecified inTaglibOfUri:<% @ Taglib uri = "/helloworld" prefix = "mytag" %>.
(2) InWeb. xmlConfiguringTag-location:
<Taglib>
<Taglib-Uri>/helloworld </taglib-Uri>
<Taglib-location>/WEB-INF/helloworld. TLD </taglib-location>
</Taglib>
(3) InTag-locationSpecified in. TLDProcessing class defining implementation labels in the file:
<Short-name>Mytag</Short-name>
<Tag>
<Name>Helloworld</Name>
<Tag-class>Mytag. helloworldtag</Tag-class>
<Body-content>Empty</Body-content>
</Tag>
(4)Execution Processing classMytag. helloworldtagOfDostarttag and doendtag methods, and then input the results to JSP, and output the results together with the content in JSP. In fact, the custom tag and other content in JSP are compiled into servlet by webserver.
2. Directory structure of the completed test
ApplicationMyjspPut inTomcatOfWebapps.
MyjspContainsJ2EEStandard Directory structure:WEB-INFAndHello. jsp.WEB-INFContains subdirectoriesClassesAndLibAndWeb. XML, TLDFiles can be stored inWEB-INFCan also be placed inWEB-INF.
3 . Start the experiment 3 . 1 . Write JSP
<! -Hello. jspSource code-->
<% @ Page contenttype = "text/html; charset = GBK" %>
<% @ Taglib uri = "/helloworld" prefix = "mytag" %>
<HTML>
<Head>
<Title>
Jsp1
</Title>
</Head>
<Body bgcolor = "# ffffc0">
<H1>
The content in the custom tag is shown below
</H1>
<Br>
<Mytag: helloworld> </mytag: helloworld>
<Br>
</Form>
</Body>
</Html>
3 . 2 . Write Web. xml <! -Web. xml Source code -->
<? XML version = "1.0" encoding = "UTF-8"?>
<! -- Edited with xmlspy V5 Rel. 4 U (http://www.xmlspy.com) by Williams (501) -->
<! Doctype web-app public "-// Sun Microsystems, Inc. // DTD web application 2.3 // en"
Http://java.sun.com/dtd/web-app_2_3.dtd>
<Web-app>
<Taglib>
<Taglib-Uri>/helloworld </taglib-Uri>
<Taglib-location>/WEB-INF/helloworld. TLD </taglib-location>
</Taglib>
</Web-app>
3 . 3 Write TLD File
<! -Helloworld. TLDSource code-->
<? XML version = "1.0" encoding = "ISO-8859-1"?>
<! 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>Mytag</Short-name>
<Tag>
<Name>Helloworld</Name>
<Tag-class>Mytag. helloworldtag</Tag-class>
<Body-content>Empty</Body-content>
</Tag>
</Taglib>
Jsp2.0
<? XML version = "1.0" encoding = "UTF-8"?>
<Taglib version = "2.0" type = "taglib" text = "taglib">
<Description> test </description>
<Tlib-version> 1.0 </tlib-version>
<Tag>
<Description> Hello World </description>
<Name> title </Name>
<Body-content> Empty </body-content>
<Tag-class> com. Test. Tag. helloworld </Tag-class>
<Attribute>
<Name> title </Name>
<Required> true </required>
<Rtexprvalue> true </rtexprvalue>
</Attribute>
</Tag>
</Taglib>
3 . 4 Compile the label implementation class
<! -Label implementation classHelloworldtag. ClassSource code-->
Package mytag;
Import java. Io. ioexception;
Import javax. servlet. jsp .*;
Import javax. servlet. jsp. tagext .*;
Public classHelloworldtagExtendsTagsupport{
Public helloworldtag (){
}
Public intDostarttag ()ThrowsJsptagexception{
ReturnEval_body_include;
}
Public intDoendtag ()ThrowsJsptagexception{
Try {
Pagecontext. Getout (). Write ("Hello World ");
}
Catch (ioexception ex ){
Throw new jsptagexception ("Error");
}
ReturnEval_page;
}
}
3 . 5 Execution result
DeployTomcatOfWebappsDirectory, startTomcat, Enter:JSP/Hello. jsp "> http: // localhost: 8080/myjsp/Hello. jsp
Posted @James. Ying reading (...) Comment (...) EDIT favorites
Refresh comment refresh page Back to Top
Blog homepage blog news flash programmer recruitment Knowledge Base
Announcement
Copyright 2013 James. Ying