JSP2 Tag Learning Notes

Source: Internet
Author: User
Tags definition count empty reference tld root directory
Js| Notes JSP2.0 Label Learning notes
1. Notes
This document is to learn the examples of Tomcat, and no reference to the corresponding documentation, so the correctness of this article is not guaranteed. If there is a wrong place, may wish to email to 8280338@tzenet.com, we common progress.

2. JSP2.0 Label Use method
The use of labels in JSP2.0 consists of several parts:

(1) Add label reference in Web_inf/web.xml

(2) The implementation of the tag, may be class or web Templet

(3) Adding references in the JSP file

In the actual use, the analysis of jsp2.0 tags can start from the JSP file, and then analyze the web.xml and corresponding tag library files.

2.2 How to use the basic label
Label reference and usage in 2.2.1 JSP file
The following is a simple JSP file:

<%@ taglib prefix= "MyTag" uri= "/web-inf/jsp2/jsp2-example-taglib.tld"%>


Description

(1) first with <%@ taglib prefix= "MyTag" uri= "..."%> describes the label prefix and the path to the label where the prefix refers, in this case 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 are using a local path, you do not need to configure the path mapping in Web.xml;

(2) After the designation of the tag prefix, you can use this prefix in the body of the JSP page to refer to the specified label in the tag library, such as &LT;MYTAG:HELLOWORLD/&GT, which refers to the helloWorld tag in the tag library specified by MyTag.

Configuration in the 2.2.2 Web.xml
If you are not using a local path in the above example, you need to configure the path transformation in Web.xml, such as: <%@ taglib prefix= "MyTag" uri= "HTTP://MYTAGLIB/MYTESTTAGS1"%> To reference the tag library, you need to add the following configuration to the Jsp-config node of the Web.xml file:

<taglib>

<taglib-uri>http://mytaglib/myTestTags1</taglib-uri>

<taglib-location>/web-inf/jsp2/jsp2-example-taglib.tld </taglib-location>

</taglib>

The JSP interpreter converts HTTP://MYTAGLIB/MYTESTTAGS1 to a local path based on the configuration in Web.xml. Of course <taglib-uri> Chinese can be any value, but can not conflict with other configurations, so it is best to prefix when planning, such as: Http://mytaglib.

Configuration in the 2.2.3 tag library
The configuration in the tag library is actually a reference to the implementation class. The JSP interpreter, based on the reference to the tag in the JSP file, finds the corresponding label in the tag library, and then invokes the specific implementation class based on the definition in the label, in this case, the reference to the label in the JSP file: <mytag: Helloworld/&gt, which refers to the helloWorld tag, which is defined in the tag library as follows:

<tag>
<description>outputs Hello, world</description>
<name>helloWorld</name>
<tag-class>jsp2.examples.simpletag.HelloWorldSimpleTag</tag-class>
<body-content>empty</body-content>
</tag>
The label is made up of four parts:

(1) Description: Describe the role of the label

(2) Name: The name of the tag, which is used in the JSP file, fairly by the class name;

(3) Tag-class: The implementation index of the label, equivalent to the implementation of the class; This example index specifies JSP2.EXAMPLES.SIMPLETAG.HELLOWORLDSIMPLETAG, a Java class file that should be placed in the web_inf/classes/ Helloworldsimpletag.class documents under the Jsp2/examples/simpletag directory;

(4) Body-content: When using a label in a JSP file, the content category in the label, the value of this example is empty, which means that there is no content in the HelloWorld label. If you add content to the tag in the JSP page, such as &LT;MYTAG:HELLOWORLD&GT;HELL&LT;/MYTAG:HELLOWORLD&GT, the page will complain, of course <mytag:helloWorld> </mytag:helloWorld> is not wrong (note that you cannot have a blank format).

Implementation of 2.2.4 Class
The class Helloworldsimpletag is implemented as follows:

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) The class of JSP tag invocation is generally inherited from Simpletagsupport

(2) The method of Getjspcontext () is implemented in Simpletagsupport, which can get the output stream pointing to the JSP page.

(3) Getjspcontext (). Getout (). Write ("Hello, world!") : Output hellow,world!

2.3 Tag configuration containing variables
Label reference and usage in 2.3.1 JSP file
The following is a JSP page that references a variable label:

<%@ taglib prefix= "MyTag" uri= "/web-inf/jsp2/my_taglib.tld"%>
<mytag:repeat num1= ">"
Invocation ${count} of 5<br>
</mytag:repeat>
</body>
Description

(1) <mytag:repeat num= "", input parameters of the incoming method: that is, in the label will be passed on as a property of the label, and specify its property value, in this case, the parameters passed in NUM1, its value is 15;

(2) ${count}, display the value of the output parameter in the page, Count is the name of the output parameter

Configuration in the 2.3.2 tag library
The corresponding configuration in the tag library is as follows:

<tag>
<description>repeats the body of the tag ' num ' times</description>
<name>repeat</name>
<tag-class>jsp2.examples.simpletag.RepeatSimpleTag</tag-class>
<body-content>scriptless</body-content>--label content is a script
<variable>
<description>current Invocation count (1 to NUM) </description>
<name-given>count</name-given>--Output parameter description
</variable>
<attribute>
<name>num</name>--Input parameter description
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
The label is made up of six parts:

(1) Description: see above

(2) Name: see above

(3) Tag-class: see above

(4) Body-content: The value of this example is scriptless, which means that the contents of the label are script

(5) Variable: output parameters, in the JSP using ${count} to refer to the output variable value, the output parameter name is not mandatory, here is just a convention, that is, the requirements repeat implementation class finally count this output parameter, in order to facilitate reference in the JSP page, If there is no error will not be;

(6) attribute: Input parameters, in the JSP using < Mytag:repeat num= "5" > to set this input parameter num, this parameter name is mandatory, that is, in the repeat implementation class has setnum this method, If there is no corresponding method JSP page will error;

Implementation of 2.3.3 Class
Package Jsp2.examples.simpletag;

Import javax.servlet.jsp.JspException;
Import Javax.servlet.jsp.tagext.SimpleTagSupport;
Import Java.util.HashMap;
Import java.io.IOException;

public class RepeatSimpleTag1 extends Simpletagsupport {
private int num;

public void Dotag () throws Jspexception, IOException {
for (int i=0; i<num; i++) {
Getjspcontext (). setattribute ("Count", string.valueof (i + 1));
Getjspbody (). Invoke (null);
} }

public void setnum (int num) {
This.num = num; }}
Description

(1) Getjspcontext (). setattribute defines the output parameter count for the output to the JSP page, where count is a string whose value may not be the same as the variable definition in the tag library, for example, it can be Len, Of course, in the JSP reference should be here, that is, the use of ${len} reference;

(2) setnum (int num) method: Receives input parameter, this method named rule is set+ input parameter name, this method takes a parameter, use to receive the value of input parameter.

2.4 Definition method of function label
function tag Reference and usage in 2.4.1 JSP file
<%@ taglib prefix= "My" uri= "/web-inf/jsp2/jsp2-example-taglib.tld"%>
<body><span>${my:caps ("HELLO World")}</span></body>


Description

(1) A reference to a function label is simpler than a reference to another label, and its reference is similar to a function invocation method in a high-level language, and the corresponding parameter value is entered directly after the parentheses after the function name, without the need for additional properties of the label to specify the input parameters.

The 2.4.2 function tag is configured in the tag library
<function>
<description>converts the string to all caps</description>
<name>caps</name>
<function-class>jsp2.examples.el.Functions</function-class>
<function-signature>java.lang.string caps (java.lang.String) </function-signature>
</function>
The description is as follows:

(1) The definition of this method is included in the function element;

(2) Name: Name of function

(3) Function-class: This function truly implements class, in this case, the CAPS function is implemented by the functions class in the Web_inf/classes/jsp2/examples/el path;

(4) Function-signature: Describe the input and output parameter types of the function without specifying the parameter name.

Implementation of 2.4.3 Class
Package jsp2.examples.el;
Import java.util.*;

public class Functions {
public static string caps (string text) {
return Text.touppercase ();
}}
Description

(1) The function tag implementation class is a normal class, it does not need to inherit from a parent class;

(2) The function tag implementation class must have the corresponding method and the corresponding function meanings in the tag library (including function name, input parameter number and input parameter type, function return value);

2.5 Label Templates
The characteristics of the label template:

(1) There is no TLD file in the JSP through the <%@ taglib prefix= "tags" tagdir= "/web-inf/tags"%> reference, that is, the use of Tagdir instead of the URI, the tags here is a folder, not a file, Include the required label template in this folder;

(2) Pass in the value of the parameter in the following ways:

<tags:displayProducts>
<jsp:attribute name= "Normalprice" >....</jsp:attribute>
<jsp:attribute name= "OnSale" >....</jsp:attribute>
.....
</tags:displayProducts>
(3) displayproducts is a filename with the/web-inf/tags directory;

(4) through <jsp:attribute name= ".... "> To assign Value

(5) A label template is a plain text file, similar to an HTML file, and does not require a Java class to implement the label definition;

(6) Personally think that the main role of the label template is in typesetting, while the other tags function in the implementation of the function; I do not intend to use the label template in future development, so this article no longer analyzes the label template.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.