Summary of the implementation process of JSP custom tag Taglib

Source: Internet
Author: User
Tags tld

Taglib instructions

The Taglib command defines a tag Library and the prefix of custom tags.

For example, the tag Library Supported by struts, the html Tag library, the bean tag library, and the logic tag library.

The specific implementation methods are not described here. We will introduce the corresponding learning materials from a macro perspective and solve the difficulties.

In addition to the struts tag library, we also commonly have the jstl tag library.

In this way, the tag library or tag library file is introduced in the jsp interface before the defined tag can be used normally.

Copy codeThe Code is as follows: <% @ taglib prefix = "bean" uri = "http://struts.apache.org/tags-bean" %>
<% @ Taglib prefix = "logic" uri = "http://struts.apache.org/tags-logic" %>
<% @ Taglib prefix = "html" uri = "http://struts.apache.org/tags-html" %>
<% @ Taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>

Advantages of custom tag Library

The custom tag library is added in jsp1.1. The custom tag library can be seen as an excellent component. In the custom tag library, we can encapsulate complex business logic functions in the tag library. Instead of writing specific code in jsp. In this way, jsp code and java coding capabilities do not have to be forcibly coupled.

When we mention the jsp version, we should first determine our jsp version, because the version is different, the header file introduced by the tag library file is different, and the tags are also different.

How to view the jsp version or servlet version we use

Open the/jsp-api.jar/MANIFEST. MF file under the META-INF under the lib file under tomcat to view the jsp version.

Because I have already imported eclispe, the figure is as follows:

Lib file under the servlet-api.jar/META-INF/MANIFEST. MF file, view the servlet version

Because I have already imported eclispe, the figure is as follows:

New Features of Jsp2

In the preceding figure, the jsp version is 2.2 and the servlet version is 3.0. Currently, servlet3.0 corresponds to the jsp2.2 specification, but in general, jsp2.0 and jsp2.2 are collectively referred to as jsp2.

If jsp2 is used, web. xml uses a version later than servlet2.4. Let's take a look at the header file corresponding to web. xml:

New features:

Copy codeThe Code is as follows: <web-app version = "4"
Xmlns = "http: // javasuncom/xml/ns/j2ee"
Xmlns: xsi = "http: // wwwworg/2001/XMLSchema-instance"
Xsi: schemaLocation = "http: // yununcom/xml/ns/j2ee
Http: // javasuncom/xml/ns/j2ee/web-app_2_xsd ">
</Web-app>

1. Directly configure jsp attributes

For example, configure in web. xml:

Copy codeThe Code is as follows: <jsp-config>
<Jsp-property-group>
<! -- Apply those files -->
<Url-pattern> * jsp </url-pattern>
<! -- Ignore el expressions -->
<El-ignored> true </el-ignored>
</Jsp-property-group>
</Jsp-config>

Of course there are many features:

2. Expression Language

Expression language, that is, el expressions.

3. Use TagFile

You can use TagFile to process class and library files instead of tags.

Header files in the label library file of jsp1 and jsp2

Header file of the label file of jsp1:

Copy codeThe Code is as follows: <? Xml version = "0" encoding = "UTF-8"?>
<! DOCTYPE taglib
PUBLIC "-// Sun Microsystems, Inc/dtd jsp Tag Library 1 // EN"
"Http: // javasuncom/j2ee/dtds/web-jsptaglibrary_1_dtd">

According to the suffix dtd, I know this is a dtd file, but what does this mean?

If a dtd file is introduced, the form of DOCTYPE is used. But what does PUBLIC "-// Sun Microsystems, Inc. // dtd jsp Tag Library 1.1 // EN" mean?

How to introduce a dtd file based on xml? You can import a dtd file in the following format:

1. Introduce dtd in the xml document

Copy codeThe Code is as follows: <? Xml version = "0"?>
<! DOCTYPE note [
<! ELEMENT note (to, from, heading, body)>
<! ELEMENT to (# PCDATA)>
<! ELEMENT from (# PCDATA)>
<! ELEMENT heading (# PCDATA)>
<! ELEMENT body (# PCDATA)>
]>
<Note>
<To> George </to>
<From> John </from>
<Heading> Reminder <Body> Don't forget the meeting! </Body>
</Note>

2. introduce external dtd files in xml documentsCopy codeThe Code is as follows: <? Xml version = "0"?>
<! DOCTYPE note SYSTEM "notedtd">
<Note>
<To> George </to>
<From> John </from>
<Heading> Reminder <Body> Don't forget the meeting! </Body>
</Note>

<! ELEMENT note (to, from, heading, body)>
<! ELEMENT to (# PCDATA)>
<! ELEMENT from (# PCDATA)>
<! ELEMENT heading (# PCDATA)>
<! ELEMENT body (# PCDATA)>

When declaring the document type, the SYSTEM or PUBLIC interface is used to point out the location of the external DTD file.

Use the system keyword as follows: <! DOCTYPE root element name SYSTEM "URI of the external DTD file">

Use public as follows: <! DOCTYPE root element name PUBLIC "DTD name" "external DTD file URI">

Generally, the system keyword is used to describe a private dtd file.

The public keyword is used to describe the public dtd file. In the public format, the dtd file is searched by default based on the dtd name. If the dtd file cannot be found, the file is searched based on the uri Of The dtd file.

The header file of the label file in jsp2 is as follows: it uses the schema format instead of the dtd file.

Copy codeThe Code is as follows: <? Xml version = "0" encoding = "UTF-8"?>
<Taglib xmlns = "http: // javasuncom/xml/ns/j2ee"
Xmlns: xsi = "http: // wwwworg/2001/XMLSchema-instance"
Xsi: schemaLocation = "http: // javasuncom/xml/ns/j2ee http: // javasuncom/xml/ns/j2ee/web-jsptaglibrary_2_xsd"
Version = "0"> </taglib>

Tag Library File Location

Place the tld file in the WebContent directory or its subdirectory. In this case, you do not need to configure the location of web. xml. Tomcat directly reads tld files under webcontent.

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.