National day will come, the work is not interested in all, to summarize points in the project is usually not very modest dry.
The introduction of the front-end JS, CSS is generally the case:
<type= "Text/javascript" src= "webcontent relative path"></ script><type= "text/css" href = "webcontent relative path" rel= "stylesheet"/>
Simplified JS, CSS Introduction posture:
<path= "Static resource relative path"/><path= " Static resource relative path "/>
Seems to be more pleasing to the eye, custom tags introduced in the way the file, the benefits and extension points there are many, and listen to my slow way.
The custom label based on Jsp-api, to have not used the JSP classmate, in fact, there is no need to go down, are very busy, right?
1. Inherit tagsupport design label processing class
Javax.servlet.jsp.tagext.TagSupport as a custom label core concern class, implemented the Iterationtag, tag, Jsptag interface.
In these interfaces that are implemented, some constants that represent states need to be introduced so that your understanding will be brighter.
int // Skip the code between the start and end tags int Eval_body_include = 1; // The label body needs to be processed int Skip_page = 5; // ignore the rest of the page int Eval_page = 6; // continue to output the following page int Eval_body_again = 2; // repeated execution of the method in place
With me this activity diagram and the above status code and then combined with the interface method, should generally understand the sun bottom of the JSP tag the entire process of processing it.
Like struts <s:> tag series, WebWork <ww:> tag series, JSTL <s:> tag series, etc... Are extensions that are made under the above process.
Well, the bottom mechanism anatomy ends, or the regression theme, the custom label processing class that inherits TagSupport is as follows.
Public classStyletagextendsTagSupport {PrivateString Path; PublicStyletag () {} Public intDoendtag ()throwsjspexception {jspwriter writer= This. Pagecontext.getout (); String ContextPath= This. Pagecontext.getrequest (). Getservletcontext (). Getcontextpath (); Try { if(Strutil.isnotblank (path)) {if( This. Path.startswith ("/") {writer.write ("<link rel= ' stylesheet ' type= ' text/css ' href= '" + ContextPath + "/static" + This. Path + "'/>"); } ElseWriter.write ("<link rel= ' stylesheet ' type= ' text/css ' href= '" + This. Path + "'/>"); } } Catch(Throwable var9) {System.out.println ("Output style Error:" +var9.getmessage ()); } finally { This. Path =NULL; } returnTagsupport.eval_page; }//....getter/setter}
What I want to do is relatively simple, here is enough to rewrite the Doendtag method, the actual project scenario involves complexity, not described here.
2. Writing the TLD tag library definition
When you want to use the JSP page, you also need to write the XML tag definition that corresponds to the backend processing class.
<?XML version= "1.0" encoding= "UTF-8"?><taglibxmlns= "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"> <Description>Project Core Tag Library</Description> <Display-name>JSTL functions Core</Display-name> <tlib-version>1.1</tlib-version> <Short-name>Fnc</Short-name> <URI>Http://com.rambo.spm/core/tags</URI> <Tag> <Description>Simplifying how CSS is introduced in the page</Description> <name>Style</name> <Tag-class>Com.rambo.spm.core.tag.StyleTag</Tag-class> <body-content>Empty</body-content> <attribute> <Description>The path of the CSS relative to static</Description> <name>Path</name> <Required>True</Required> <Rtexprvalue>True</Rtexprvalue> </attribute> </Tag></taglib>
Introduction Method: (The above tag detailed attribute and use method: http://blog.sina.com.cn/s/blog_76b2c4810101a8so.html)
<!--Relative Path Introduction -<%@ taglib Prefix="FNC"URI="/web-inf/tlds/core.tld" %><!--Unique URL Ingestion -<%@ taglib Prefix="FNC"URI="Http://com.rambo.spm/core/tags" %>
OK, under the premise of understanding the underlying processing flow, the specific project specific scenarios can be customized label design.
The purpose of designing labels is of course to simplify the front end, integrate common functions, accelerate project advancement, of course, the design needs to be precipitated and accumulated.
How to use custom tags to simplify JS, CSS introduction?