JSP2.0 Label Learning Notes 1. Description This document is a notebook that learns Tomcat, does not refer to the corresponding documentation, so it cannot guarantee the correctness of this paper. If there is something wrong, you may wish to advance into 8280338@tzenet.com. 2. JSP2.0 Label Usage Method JSP2.0 The use of tags in JSP2.0 consists of multiple parts: (1) Web_inf / web.xml Add label reference (2) Tag implementation, may be Class or Web Templet (3 Adding a reference in the JSP file In actual use, analyzing the JSP2.0 tag first starts with the JSP file, then analyze the Web.xml and the corresponding label library file. 2.2 Basic Label Usage 2.2.1 Label References in JSP Files and Usage The following is a simple JSP file: <% @ Taglib prefix = "MyTag" URI = "/ Web-INF / JSP2 / JSP2-EXAMPLE-TAGLIB .TLD "%>
body> html> Description: (1) First use <% @ taglib prefix =" MyTag "URI =" ... "%> Description label The prefix and the path where the prefix is referenced, in this example, the URI value is a relative path (local path), that is, the current project root directory WEB-INF / JSP2 / JSP2-EXAMPLE-TAGLIB.TLD. If you use a local path, you don't need to configure the path map in Web.xml; (2) After specifying the label prefix, you can use this prefix in the Body in the JSP page to reference the specified label in the label library, such as
is the HelloWorld tab in the label library specified by MyTag. 2.2.2 Configuration in Web.xml If it is not a local path in the above example, you need to configure path conversion in web.xml, such as use: <% @ taglib prefix = "MyTag" URI = "http: / / mytaglib / mytesttags1 "%>" To reference the label library, you need to add the following configuration in the JSP-Config node of the web.xml file:
http: // mytaglib / mytesttags1 taglib-URI > /web-inf/jsp2/jsp2-example-taglib.tld taglib-location> taglib> JSP interpreter will transform http: // mytaglib / mytesttags1 in Web.xml For the local path. Of course, the text in can be any value, but cannot conflict with other configurations, it is best to add prefix when planning, such as http: // mytaglib, etc.
2.2.3 Configuration in the label library In the label library, the configuration is actually a reference to the implementation class. The JSP interpreter will find the corresponding label in the label library according to the reference to the label in the JSP file, and then according to the label The definition of the definition calls the specific implementation class. In this example, the tag is referenced in the JSP file: , that is, the HelloWorld tag is referenced, this label is defined in the label library as follows: OUTPUTS HELLO, World Description> HelloWorld name> jsp2.examples.simpletag.helloworldsImpleEtAg tag-class> EMPTY body-content> tag > This label consists of four parts: (1) Description: Describe the role of the tag (2) Name: The name of the label, the name is used in JSP file, which is quite named; (3) tag-class: Tag implementation Index, equivalent to class implementation; this example index specifies jsp2.examples.simpletag.HelloWorldSimpleTag, is a Java class file, should be placed in a web_inf / class / jsp2 / example of helloWorldsImpleTag.class file; (4) Body-content: When using the label in the JSP file, the contents of the tag, the value of this example is EMPTY, indicating that there is no content in the tag of HelloWorld. If you add content in this tag in the JSP page, such as Hell mytag: helloworld>, the page will report an error, of course mytag: helloworld> is not wrong (note No space format). 2.2.4 class implementation class HelloWorldSimpleTag achieve the following: package jsp2.examples.simpletag; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.SimpleTagSupport; import java.io.IOException; public class HelloWorldSimpleTag extends SimpleTagSupport {Public void dotag () throws jspexception, ioException {getJspContext (). GetOut (). Write ("Hello, World!");}} Description: (1) JSP tag calls typically inherited in SimpleTagsupport (2) SimpleTagsupport GetjspContext () method is implemented, which can get the output stream pointing to the JSP page.
(3) getJspContext (). GetOut (). Write ("Hello, World!"): Output Hellow, World! 2.3 Tag Configuration in the TAG Configuration of Variables 2.3.1 Label references and usage methods in the JSP file The following is quoted Variable label JSP page: <% @ taglib prefix = "MyTag" URI = "/ Web-INF / JSP2 / MY_TAGLIB.TLD"%>
invocation $ { Count} of 5
mytag: repeat> body> html> Description: (1) , input parameters Incoming method: ie, will be passed in the tab The parameter is made as a attribute of the label and specifies its property value. In this example, the parameter NUM1 is incorporated, its value is 15; (2) $ {count}, display the value of the output parameter in the page, count is output Name of parameters 2.3.2 The corresponding configuration in the configuration label library in the label library is as follows: Repeats the body of the tag 'Num' Times description> repeat name> jsp2.examples.simpletag.repeatsimpletag tag-class> scriptless body-content> - tag content is script Current Invocation Count (1 to num) description > count name-given> - Output Parameter Description variable> Num name> - Input Parameter Description True Required> True RTEXPRVALUE> attt Ribute> tag> This label consists of six parts: (1) DESCRIPTION: See above (2) Name: See above (3) Tag-Class: See above (4) Body-content: This example The value is Scriptless, which indicates that the content in the label is a script (5) Variable: output parameter, using $ {count} in JSP to reference the value of this output variable, the output parameter name is not mandatory, it is just a convention,
That is, the Repeat implementation class finally has a count this output parameter so that it is convenient to reference in the JSP page, if not, it will not be reported; (6) Attribute: Enter the parameters, use $ {My: Caps ("Hello World"} span> body> html> Description: (1) Function Tags are simpler than other tags, it Quote Similar to the function call method in the advanced language, enter the corresponding parameter value after entering the parentheses after the function name, and other properties of the label are not required to explain the input parameters.
2.4.2 Function Label Configuring Converts The String To All Caps description> Caps name> jsp2.examples.el.functions function- Class> java.lang.String Caps (java.lang.string) function> Description: (1) The definition of this method is included in the function element; (2) Name: Function Name (3) Function-Class: This function is real implementation class. In this example, the CAPS function is implemented by the Functions class in the web_inf / classes / jsp2 / example of the web_inf / class; (4) Function-Signature : Describe the input and output parameter type of the function, and do not specify the parameter name.