Java EE custom Tags: creation of label classes, creation of TLD profiles (location, how to create), web-xml configuration, JSP applications

Source: Internet
Author: User
Tags closing tag html tags tld
1.Labels
Encapsulation of java code implemented in a manner similar to html tags.
First: Formed a technical standard for developing labels --- a technical standard for custom labels.
Second: the Java standard tag library (a collection of tags developed by Sun before its own), jstl, and the expression language EL.
 
Custom labels
(1) Understanding:
Can allow users to develop their own technical specifications of labels according to their own needs.
Popular: On the Jsp page, the operation of java code is encapsulated with simple tags.
// In the custom tag class, first call setPageContext () to instantiate the built-in object:
JavaEE custom tags: creation of tag class, creation of tld configuration file (location, how to create), Web-XML configuration, JSP application
// Then doStartTag () method, the core code is placed in this method:
JavaEE custom tags: creation of tag class, creation of tld configuration file (location, how to create), Web-XML configuration, JSP application
 // Define variables, Get and Set methods of variables:
 private String UserName = "";
JavaEE custom tags: creation of tag class, creation of tld configuration file (location, how to create), Web-XML configuration, JSP application
// tld configuration files are placed in the WEB-INF directory, the main tags are as follows, and the main configuration is as follows:
// The previous tlib-version version number, Jsp-version JSP version number, short-name namespace, are required
// uri, note that uri is not url, url is the path to visit.
// And uri is the alias when accessing, the alias of the tag written on the JSP page.
// name is an alias for the tag class, and tag-class is the physical path where the tag class is stored
// body-content is whether there is a tag body, if it is / the child close tag is set to EMPTY
// If it involves tag attributes, you can set attribute, where name is the name of the attribute, not an alias, whether the required attribute must be set, and whether rtexprvalue assigns a value to the attribute when the program runs.
JavaEE custom tags: creation of tag class, creation of tld configuration file (location, how to create), Web-XML configuration, JSP application
// If uri is not set in the tld file, then talib-uri must be set in web-xml.
JavaEE custom tags: creation of tag class, creation of tld configuration file (location, how to create), Web-XML configuration, JSP application
// In the JSP page, use the taglib instruction to import custom tags.
JavaEE custom tags: creation of tag class, creation of tld configuration file (location, how to create), Web-XML configuration, JSP application
// Used on the JSP page, my is the namespace defined for the tag class in the tld file, and hello is the alias of the tag class specified in the tld file:
JavaEE custom tags: creation of tag class, creation of tld configuration file (location, how to create), Web-XML configuration, JSP application

(2) Technical composition
A: Label processing class: Java class that implements all information display functions
B: tag configuration file (tag description file, library file): is a tld file (extension TLD) that complies with the XML specification
C: configure web.xml: tell the web application that other tag information needs to be referenced
D: On the jsp page, call the label
 
3. Characteristics of the label
(1) Classification
A: Tags appearing in pairs
B: self-closing label
(2) Features
A: The label must be closed properly
B: The label cannot have the same name
C: The label may or may not have a label body
D: Tags can have attributes, but a tag cannot have attributes with the same name
 
The simplest label: a label with no label body and no attributes
 
4. Custom label development process (illustration above)
A: Define what you want to achieve: what you want to display on the page
B: Development of tag processing classes
C: Create and configure tag description file: TLD file
D: Configure web.xml
E: jsp page call tag
 
5.Develop tag processing classes
(1) Parent class
Two classes per interface
Interface: Tag interface javax.servlet.jsp.tagext.Tag
class:
Self-closing: TagSupport
Appears in pairs: BodyTagSupport
(2) Developing tags
Integrate from the specified class, override setPageContext (), doStartTag (), doEndTag ().
Important: tag start operation doStartTag () method ★
 
6. Create a tag configuration file (TLD file)
(1) Function
Is an XML file used to manage multiple tag processing classes in a unified manner.
(2) Create
Has its own format and specifications (same as XML files)
It is recommended to create your own tld file in the WEN-INF directory.
(3) File format
A: Root element: taglib
B: the four starting child elements of taglib
<tlib-version> Technical standard version 1.0 of the tag library </ tlib-version>
<jsp-version> Supported technical standard version 1.2 of jsp </ jsp-version>
<short-name> Namespace of tag library file: abbreviation </ short-name>
<uri> Uniquely marked symbol (alias) of the current tag library file </ uri>
 
 
Namespace: A shorthand name used to distinguish which tag library the current tag belongs to. <jsp: useBean>
 
Note: The uri child element is optional, but it will affect the later calls.
 
C: tag child element
Each tag processing class must be configured in a tld file. Each tag class corresponds to a tag element, describing its own configuration information.
<tag>
     <name> Alias for tag processing class </ name>
     <tag-class> The physical address of the tag class </ tag-class>
     <body-content> Whether there is a tag body </ body-content>
</ tag>
 
body-content: The default value is jsp. If it is a self-closing tag, it needs to be set to empty.
If the tag has attributes, there are attribute child elements.
 
7.Configure web.xml
(1) Function
Set the tag configuration file in web.xml to tell the web application where the custom tags need to be called.
(2) Premise
In the tag configuration file, if <uri> is not configured, you must configure web.xml. If you have configured the value of the uri element, you do not need to configure web.xml, you can call it directly on the jsp. The uri manifests that when a tag is introduced in a JSP page, it is necessary to specify a uri for the taglib instruction. Note that it is distinguished from the alias name of the tag class in the TLD file, which is written after the namespace when the tag is called in the JSP page.
(3) Configuration (as shown above in WEB.XML)
The configuration of all tag configuration files must be in the <jsp-config> element. Each configuration file has its own <taglib> element.
<taglib>
<taglib-location> The physical address of the tag configuration file </ taglib-location>
<taglib-uri> Call alias for tag configuration file </ taglib-uri>
</ taglib>
 
8, jsp call label
(1) Introduce tag library
Introduce using taglib instruction
<% @ taglib uri = "Tag library alias" prefix = "Tag library prefix (namespace)"%>
uri: If the uri is already configured in the tld file, it must be the same as the turi uri. If not configured in tld, it must be consistent with taglib-uri in web.xml.
prefix: The prefix. If a short-name is set in the tld file, it must be consistent with the short-name.
 
(2) Call label
<Prefix: alias for tag class>
 
(3) Life cycle of tag processing class
A: Load
According to the alias of the tag class, locate the tag processing class and load it on the container
B: instantiation
Instantiated according to the default parameterless constructor
C: Setting the context
Set page context and get built-in objects
D: Set parent label
E: Perform the operation starting from the label
F: perform the end of label operation
G: resource release
 
9, jsp and tag information processing methods
Two types:
A: With the help of the session attribute, the jsp passes the value to the tag processing class.
B: With the help of the attribute of the tag, the value is passed.
 
10.Attribute
(1) Understanding
A collection of names and values used to attach basic information to the label.
(2) Classification
Required attributes
Optional attributes
(3) Features
A: is a collection of names and values
B: If there is an attribute, the attribute must have a value
C: The value of the attribute must be enclosed in quotes
D: A label cannot have the attribute with the same name
(4) Setting
A: In the tag processing class, create a variable representing the attribute and the corresponding set / get method (see the figure above).
In tag processing classes, attributes exist as variables.
B: Use the variable directly in the label start action
C: On the tag configuration file, in the corresponding tag, add the attribute configuration.
Each attribute has its own <attribute> tag
<attribute>
  <name> The name of the attribute (same name as the variable in the class) </ name>
  <required> Whether the attribute is required (true / false) </ required>
  <rtexprvalue> Whether the value of the attribute is assigned at runtime (program dynamic assignment) </ rtexprvalue>
</ attribute>
D: Use attributes in tags on jsp pages
Note: The attribute name must be the same as the variable in the class.
Related Article

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.