Custom Taglib development

xiaoxiao2021-04-06  304

Custom labels must implement one of the three interfaces below: Tag, IterationTag, Bodytag

1.TAG If you want to implement this interface, you can write your own way by extending the tagsupport class, without having to implement all the methods in the TAG interface. TAG interface method: dostarttag (), doendtag (), getParent (), setParent (), release (), setPageContext () in the TAG class code cannot be used as JSP, directly use Out implicit object, he has an object Using PageContext, you can get an OUT object through its getout () method. Inside the label, access any implied objects is by calling the pageContext's set method.

The 2.IpectArationTag ITAG interface is similar to the TAG interface for a custom label that needs to be repeatedly calculated to calculate its code body. It expands the TAG interface and implements a new method DoafterBody () to implement a loop. This method is called only from DostartTag () returning Eval_Body_include. When executing the DOAFTERBODY () method, if returned is evAl_body_again, the DOAFTERBODY () method will be executed again until DoafterBody () returns Skip_Body or Eval_Body_include.

3. Bodytag BodyTag interface extends ItemionTAG and provides functions for operating the code body content. That is to modify the resulting code body when calculating the code body. The BodyContent object is the result of saving the calculation of custom labels. It has a new method DOINITBODY (), this method is only called when the dostarttag () method returns Eval_Body_buffered, and it will create a Bodycontent object save result.

Extended Custom Tags: Add Properties First, add an attribute element in the TLD file, then you need to define this property and its setter method in the Java file. Attribute element has four sub-elements from , , , , here indicates whether the attribute accepts the calculation results of the Scriptlet expression, default False, It can only accept static values.

Adding variables can join a element in a TLD file, which includes represents the name of the save variable, represents the Java type of the variable, Boolean Indicates whether this variable is new, The range of use of variables (at_begin means that the at_end represents the start tag and the termination tag from the termination label. After defining the variables, you need to use this variable in the Java file; "", Object); here the Key value should be the name of the variable.

With the TAGEXTRAINFO (TEI) class, there are two types of objects that can be used, tagdata (information for label properties), VariableInfo (Description Code Variable), one TAGEXTRAINFO class code: public variableinfo [] getvariableinfo (tagdata data) {string variablename = data.getAttributeString ( "name"); VariableInfo vi = new VariableInfo (variableName, "String []", true, VariableInfo.AT_END); VariableInfo [] tagVariables = new VariableInfo [1]; tagVariables [0] = vi; return tagVariables } You can get a value of a certain attribute through the GetAttributeString method of the TagData class, and another method GetAttribute is also a value that gets an attribute but returns an object. The GetVariableInfo method must return a VariableInfo array. In addition, it is also necessary to define elements in the TLD, and add a element to indicate the full name of the TEI class. The method contained in the PageContext object includes: getOut (); getPage (); getRequest (); getResponse (); getServletConfig (); getServletContext (); getSession ();

Return constant meaning in the TAG interface: evAl_body_include: Tell the content of the server, and send these content into the output stream Skip_body: Tell the server not to process the text content Eval_Page: Let the server continue to perform the page Skip_page: Let the server do not handle the remaining page eval_body_again: Let the server continue to process the text content, only the DOAFTERBODY method can return the field of the evAl_body_buffered: bodytag interface, return Eval_Body_include, Skip_body, Skip_body, Skip_page, returned by DostartTag (), and Skip_PapGe, Skip_page returns.

The other two methods were called before calling the dostarttag () method: setPageContext () and setParent (); so PageContext and Parent objects can be used in later methods, if needed.

Let the custom label creates an object in the page You must use a standard JSP object TAGEXTRAINFO class. It can create script variables. You can also check the label when compiling. TEI classes can only be generated by the SetAttribute method in the PageContext object. Variables are not generated separately.

Defining scripts via TEI class can allow users to define the name of the object you use in the page. In addition to using the TEI approach, you can simply define a object in TLD to use custom objects, usage as follows: name < Variable-class> string [] true at_end For the child elements of Variable, refers to the creation The variable name is obtained from the attribute Name, and of course, the name of the variable can be limited via the element. Note that these two elements are mutually exclusive. The life cycle of a custom tag for extended bodytagsupport is as follows: 1. Create a tag 2. Call the setter method 3. Call the dostartTAG () method 4. Call the setBodyContent () method 5. Call the initbody () method 6.DoAfterBody (); According to the return value, if it is evAl_body_again, continue 6, if not, execute 88. Call the DOENDTAG () method 9. If the mark is required to be reuse, if necessary, execute 4; otherwise execute the Release () method.

The method of the Tagsupport class FindanceStorwithClass () method can be used to find the specified parent class, which has two parameters a class name for itself, and one is the name of the parent class to find, if not returned; for example, ParentTag Parent = (PARENTTAG) THIS.FINDANCESTORWITHCLASS (this, Parenttag.class);

Custom Tag Validation Method: JSP1.1 TEI class can test your own tag at compile time, there is an isvalid () method in this class. If this tag is defined for this TEI class in TLD, then the web page will be compiled. This method will be called and will be incremented to the parameter tagdata containing the specific content of the attribute. (Equally valid in JSP1.2) JSP1.2 JSP1.2 introduces a new tag test method to define a new class TaglibraryValidator, and can be used to give a class of inspection markings, most of the cases only use this class. Validate () method, it has three parameters: prefix (prefix defined in taglib instruction); URI (URI in the TLD file); Page (PageData XML version of the JSP page), the validate () method returns a value null Indicates that the verification is successful, otherwise the returned String type will be an error message. When Validator is defined in the TLD file, it should be placed outside the element definition because it is used to process all the tags in the verification tag library. .

Methods in JSP1.2 and JSP1.1: TaglibraryValidator is more comprehensive than TEI classes, can be used to detect the entire web page, not just marking itself, can be used to handle cooperation between tags, and this method can be used to notify Where is the programmer error, but it is also more complicated than the TEI class method because it needs to traverse the entire XML version of JSP (complete the getAttributeValue method). JSP1.2 TrycatchFinally interface: This interface is mainly used to release the resource used in custom tags when there is an abnormality, it defines two methods: public void Docatch; (when DostartTag, This method is called when DONITBODY, DOAFTERBODY, and DOENDTAG methods will call this method); () PIBLIC VOID DOFINALLY (); (When DOENDTAG is called, this method is called whether there is an exception, just like the finally block in the program, can be used to release resources)

In JSP 1.2, you can specify your own name in the TAGLIB instruction in the TLD file, and then put this TLD file with Manifest.mf in the meta-inflic In the page, these TLDs can be imported very conveniently in the page.

Principles for custom tags: 1. Use script variables (allowing designers to be named for script variables, minimizing script variables, using a combination script object and access function, using javabean) 2. When designing each other Try to avoid creating a new set of languages ​​when tagging, try to use script variables 3. Write code rather than content, do not generate HTML in custom tags, which will lose versatility

转载请注明原文地址:https://www.9cbs.com/read-132306.html

New Post(0)