JSP tag system and custom JSP tags

Source: Internet
Author: User
Tags tld

I. jsp tag system

 

Ii. Tag execution process:

 

 

III.Custom tags;

3.1 The main role of a custom tag is to remove the Java Script in JSP. It belongs to JSP Technology

3.2. Procedure for compiling a custom tag: (definition of a traditional tag)
3.3 write a class to directly or indirectly implement javax. servlet. jsp. tagext. Tag. Generally, the tag interface is indirectly implemented by inheriting tagsupport.

3.4 overwrite the corresponding methods in the parent class.
Package com. itheima;

Import java. Io. ioexception;
Import java. Io. printwriter;

Import javax. servlet. servletrequest;
Import javax. servlet. jsp. jspexception;
Import javax. servlet. jsp. jspwriter;
Import javax. servlet. jsp. tagext. tagsupport;

Public class showiptag extends tagsupport {

@ Override
Public int dostarttag () throws jspexception {
Servletrequest request = pagecontext. getrequest ();
String IP = request. getremoteaddr ();
Jspwriter out = pagecontext. getout ();
Try {
Out. Write (IP );
} Catch (ioexception e ){
E. printstacktrace ();
}
Return super. dostarttag ();
}

}
 3.5 create an XML file with the TLD extension under the WEB-INF to describe the custom tag processing class
<? XML version = "1.0" encoding = "UTF-8"?>
<Taglib xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
Version = "2.0">
<Description> slkdjfdsjfdsfjlks. </description>
<Tlib-version> 1.0 </tlib-version>
<Short-Name> itheima </short-Name>
<URI> http://www.itheima.com/jsp/myjstl </uri>
<Tag>
<Description> show remote IP </description>
<Name> showip </Name>
<Tag-class> com. itheima. showiptag </Tag-class>
<Body-content> Empty </body-content>
</Tag>
</Taglib>
 3.6 use the taglib command in JSP to introduce custom labels and then use

 

Iv. Simple labels
1. Generally, You Need To Inherit simpletagsupport and overwrite the dotag method.
2,
A. the control part is not output
<Demo1>
ABCD
</Demo1>

The dotag () method does nothing.
To output: getjspbody (). Invoke (null)
B. the JSP content behind the control tag is not output
Throw new skippageexception ();
C. Control repeated execution of subject content
Call getjspbody (). Invoke (null) cyclically in the dotag Method)
Property: the setter method must be available and described in the TLD file.
D. Modify the subject content before outputting it.
Getjspbody (). Invoke (null) to output the content to a buffered stream.
Stringwriter
After modification, it is output through (pagecontext) getjspcontext). getwrtier ()

5. Main tags and attributes of TLD files
Short-Name: prefix of the referenced tag
Uri: the address of the URI to which the TLD file is bound. You can immediately help a namespace
Tag: description tag
Name: Label name
Tag-class: The full name of the tag processing class corresponding to the tag
Body-content: Type of the label body content
Empty: no subject content
JSP: Subject content (for traditional tags)
Scriptless: Subject content (for simple tags)
Attribute: Describes the attributes of a tag.
Name: attribute name
Required: Required attribute
Rtexprvalue: whether the attribute value supports expressions (El and Java expressions)

 

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.