📄 package-summary.html
字号:
out = this.jspContext.getOut(); } else { out = this.jspContext.pushBody( writer ); } // Step F.5 - Evaluate body of fragment: try { out.write( "Fragment Template Text " ); out.write( jspContext.getExpressionEvaluator().evaluate( "${var1}", java.lang.String.class, vResolver, fMapper, "my" ) ); } finally { // Step F.6 - Restore value of JspContext.getOut() if( writer != null ) { this.jspContext.popBody(); } } // Step F.7-F.9 done in tag file (see following example) } } );// Step T.5 - Determine and set body of the tag// - body of tag_jsp_mySimpleTag.setJspBody( // Step C.1 - New instance of fragment created // Step C.2 - Store jspContext // Step C.3 - Association with nearest enclosing Tag instance new JspFragmentBase( jspContext, _jsp_mySimpleTag ) { public void invoke( java.io.Writer writer ) { javax.servlet.jsp.JspWriter out; // Step F.1-F.3 done in tag file (see following example) // Step F.4 - If writer provided, push body: if( writer == null ) { out = this.jspContext.getOut(); } else { out = this.jspContext.pushBody( writer ); } // Step F.5 - Evaluate body of fragment: try { out.write( "Body of tag that defines an AT_BEGIN\n" + " scripting variable " ); out.write( jspContext.getExpressionEvaluator().evaluate( "${var1}", java.lang.String.class, vResolver, fMapper, "my" ) ); out.write( ".\n" ); } finally { // Step F.6 - Restore value of JspContext.getOut() if( writer != null ) { this.jspContext.popBody(); } } // Step F.7-F.9 done in tag file (see following example) } } );// Step T.6 - Inovke doTag// Step T.7 occurs in the tag file (see following example)// Step T.8 - doTag returns - page will catch SkipPageException._jsp_mySimpleTag.doTag();// Step T.9 - Declare AT_BEGIN and AT_END scripting variablesString var1 = (String)jspContext.findAttribute( "var1" );</pre><h4>Generated Simple Tag Handler (MySimpleTag.java)</h4><pre>public class MySimpleTag extends javax.servlet.jsp.tagext.SimpleTagSupport{ // Attributes: private String x; private String y; private String nonfragment; private javax.servlet.jsp.tagext.JspFragment frag; // Setters and getters for attributes: public void setX( Stirng x ) { this.x = x; } public String getX() { return this.x; } public void setY( String y ) { this.y = y; } public String getY() { return this.y; } public void setNonfragment( String nonfragment ) { this.nonfragment = nonfragment; } public String getNonfragment() { return this.nonfragment; } public void setFrag( javax.servlet.jsp.tagext.JspFragment frag ) { this.frag = frag; } public javax.servlet.jsp.tagext.JspFragment getFrag() { return this.frag; } protected JspContext jspContext; public void setJspContext( JspContext ctx ) { super.setJspContext( ctx ); // Step T.2 - A JspContext wrapper is created. // (Implementation of wrapper not shown). this.jspContext = new utils.JspContextWrapper( ctx ); } public JspContext getJspContext() { // Step T.2 - Calling getJspContext() must return the // wrapped JspContext. return this.jspContext; } public void doTag() throws JspException { java.lang.Object jspValue; JspContext jspContext = getJspContext(); JspContext _jsp_parentContext = SimpleTagSupport.this.getJspContext(); try { javax.servlet.jsp.JspWriter out = jspContext.getOut(); // Create page-scope attributes for each tag attribute: this.jspContext.setAttribute( "x", getX() ); this.jspContext.setAttribute( "y", getY() ); this.jspContext.setAttribute( "nonfragment", getNonfragment() ); this.jspContext.setAttribute( "frag", getFrag() ); // Synchronize AT_BEGIN variables from calling page if( (jspValue = _jsp_parentContext.getAttribute( "var1" )) != null ) { jspContext.setAttribute( "var1", value ); } else { jspContext.removeAttribute( "var1", JspContext.PAGE_SCOPE ); } // Tag template text: out.write( "\n\n\n\n\n\n\n\nSome template text.\n" ); // Invoke c:set - recognized tag handler from JSTL: jspContext.setAttribute( "var1", jspContext.getExpressionEvaluator().evaluate( "${x+y}", java.lang.String.class, jspContext, prefixMap, functionMap, "my" ) ); // Invoke the "frag" fragment: // Step F.1 - Set values of AT_BEGIN and NESTED variables // in calling page context. if( (jspValue = jspContext.getAttribute( "var1" )) != null ) { _jsp_parentContext.setAttribute( "var1", value ); } else { _jsp_parentContext.removeAttribute( "var1", JspContext.PAGE_SCOPE ); } // Step F.2 - varReader is specified, generate a writer. java.io.Writer _jsp_sout = new java.io.StringWriter(); // Step F.3 - Invoke fragment with writer getFrag().invoke( _jsp_sout ); // Step F.4 - F.6 occur in the fragment (see above) // Step F.7 - fragment returns // Step F.8 - varReader specified, so save to var jspContext.setAttribute( "var1", new StringReader( _jsp_sout.toString() ) ); // Step F.9 - Done! out.write( "\n\nInvoke the body:\n" ); // Invoke the body of the tag: // Step F.1 - Set values of AT_BEGIN and NESTED variables // in calling page context. if( (jspValue = jspContext.getAttribute( "var1" )) != null ) { _jsp_parentContext.setAttribute( "var1", value ); } else { _jsp_parentContext.removeAttribute( "var1", JspContext.PAGE_SCOPE); } // Step F.2 - varReader is not specified - does not apply. try { // Step F.3 - Invoke body, passing optional writer getJspBody().invoke( null ); } finally { // Steps F.4 - F.6 occur in the fragment (see above) // Step F.7 - fragment returns } // Step F.8 does not apply. // Step F.9 - Done! } finally { // Tag handlers generate code to synchronize AT_BEGIN with // calling page, regardless of whether an error occurs. if( (jspValue = jspContext.getAttribute( "var1" )) != null ) { _jsp_parentContext.setAttribute( "var1", value ); } else { _jsp_parentContext.removeAttribute( "var1", JspContext.PAGE_SCOPE ); } } }}</pre><a name="translation"><h2>9. Translation-time Classes</h2></a>The following classes are used at translation time.<h3>Tag mapping, Tag name</h3><p> A taglib directive introduces a tag library and associates aprefix to it. The TLD associated with the library associates Taghandler classes (plus other information) with tag names. Thisinformation is used to associate a Tag class, a prefix, and a namewith each custom action element appearing in a JSP page.<p> At execution time the implementation of a JSP page will use anavailable Tag instance with the appropriate property settings and thenfollow the protocol described by the interfaces Tag, IterationTag,BodyTag, SimpleTag, and TryCatchFinally. The implementation guaranteesthat all tag handler instances are initialized and all are released,but the implementation can assume that previous settings are preservedby a tag handler, to reduce run-time costs.<h3>Scripting Variables</h3><p> JSP supports scripting variables that can be declared within ascriptlet and can be used in another. JSP actions also can be used todefine scripting variables so they can used in scripting elements, orin other actions. This is very useful in some cases; for example, the<code>jsp:useBean</code> standard action may define an object whichcan later be used through a scripting variable.<p> In some cases the information on scripting variables can bedescribed directly into the TLD using elements. A special case is typicalinterpretation of the "id" attribute.In other cases the logic that decides whether an action instancewill define a scripting variable may be quite complex and the name ofa <code>TagExtraInfo</code> class is instead given in the TLD. The<code>getVariableInfo</code> method of this class is used attranslation time to obtain information on each variable that will becreated at request time when this action is executed. The method ispassed a <code>TagData</code> instance that contains thetranslation-time attribute values.<h3>Validation</h3><p> The TLD file contains several pieces of information that is usedto do syntactic validation at translation-time. It also contains twoextensible validation mechanisms: a <code>TagLibraryValidator</code>class can be used to validate a complete JSP page, and a<code>TagExtraInfo</code> class can be used to validate a specificaction. In some cases, additional request-time validation will be donedynamically within the methods in the Tag instance. If an error isdiscovered, an instance of <code>JspTagException</code> can be thrown.If uncaught, this object will invoke the errorpage mechanism of JSP.<p> The TagLibraryValidator is an addition to the JSP 1.2specification and is very open ended, being strictly more powerfulthan the TagExtraInfo mechanism. A JSP page is presented via the<code>PageData</code> object, which abstracts the XML view of the JSPpage. <p> A PageData instance will provides an InputStream (read-only) on thepage. Later specifications may add other views on the page (DOM, SAX,JDOM are all candidates), for now these views can be generated fromthe InputStream and perhaps can be cached for improved performance(recall the view of the page is just read-only).<p> As of JSP 2.0, the JSP container must support a jsp:id attribute toprovide higher quality validation errors. Thecontainer will track the JSP pages as passed to the container, andwill assign to each element a unique "id", which is passed as thevalue of the jsp:id attribute. Each XML element in the XML viewwill be extended with this attribute. TheTagLibraryValidator can use the attribute in one or moreValidationMessage objects. The container then, in turn, can use thesevalues to provide more precise information on the location of anerror.<p>The prefix for the <code>id</code> attribute need not be "<code>jsp</code>"but it must map to the namespace <code>http://java.sun.com/JSP/Page</code>.In the case where the user has redefined the <code>jsp</code> prefix, an alternative prefix must be used by the container.<h4>Validation Details</h4><p> In detail, validation is done as follows:<p> <b>First</b>, the JSP page is parsed using the information in the TLD.At this stage valid mandatory and optional attributes are checked.<p> <b>Second</b>, for each unique tag library in the page as determinedby the tag library
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -