JSP Custom Label TAGLIB implementation process key summary _jsp programming

Source: Internet
Author: User
Tags tld

Introduction to Taglib Instructions

The taglib directive, in fact, is to define a tag library and the prefix of the custom label.

such as struts in support of the tag library, HTML tag library, bean Tag Library, logic tag library.

One of the specific implementation of the way, we do not introduce too much, we give you from a macro point of view and solve the problem, the following will introduce the corresponding learning materials.

In addition to struts's tag library, we often have jstl tag libraries.

This allows you to introduce tag libraries or tag library files into the interface JSP before you can use the labels defined in them.

Copy Code code 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"%>

Customizing the Tag Library benefits

A custom tag library is added to the jsp1.1 version. Custom tag libraries can be considered an excellent component. In the custom tag library, we can encapsulate the complex business logic functions in the tag library. Instead of writing specific code in the JSP. In this way, the JSP code and Java coding capabilities do not have to be forced to be coupled together.

Referring to the JSP version of the problem, we should first determine our JSP version, because the version is different, the tag library file introduced a different header file, and there are differences in the label.

How to view the JSP version or servlet version that we use

Open the/meta-inf/manifest under the Jsp-api.jar under the tomcat below Lib file. MF file to view the JSP version.

Since I have already imported the Eclispe, the figure is as follows:


LIB file under the Servlet-api.jar under the/meta-inf/manifest. MF file, view servlet version

Since I have already imported the Eclispe, the figure is as follows:

New features of JSP2

The JSP version in the above figure is 2.2,servlet version 3.0. At present, servlet3.0 corresponds to the jsp2.2 specification, but in general, we jsp2.0 and jsp2.2 are collectively referred to as JSP2.

If we use JSP2, then Web.xml is using the version above servlet2.4. Let's take a look at the Web.xml header file:

The new features are:

Copy Code code as follows:

<web-app version= "4"
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-app_2_xsd ">
</web-app>

1. Directly configure the JSP properties

For example, configure in Web.xml:

Copy Code code as follows:

< jsp-config>
<jsp-property-group >
<!--apply to those files-->
<url-pattern >* jsp</url-pattern>
<!--ignore El expression-->
<el-ignored >true </el-ignored >
</jsp-property-group >
</jsp-config>

Of course there are many features:

2. Language of expression

Expression language, which is the use of an El expression.

3. Use of Tagfile

Use Tagfile to process class and library files instead of labels.

header files in the tag library files of Jsp1 and JSP2

Jsp1 the header file for the label file:

Copy Code code 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, it is known that this is the introduction of a DTD file, but what does this specifically mean?

If you introduce a DTD file, use doctype this form. But what does public "-//sun Microsystems, Inc.//dtd JSP Tag Library 1.1//en" mean?

How do I introduce a DTD file according to XML? The introduction of a DTD file has the following forms:

1. Introducing a DTD inside an XML document

Copy Code code 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>
<body>don ' t forget the meeting!</body>
</note>

2. Introducing an external DTD file in an XML document
Copy Code code as follows:

<?xml version= "0"?>
<! DOCTYPE Note SYSTEM "NOTEDTD" >
<note>
<to>George</to>
<from>John</from>
<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 a document type, use the interstitial system or public to indicate the location of the external DTD file

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

Use public as follows: <! DOCTYPE the name of the root element public "DTD name" "URI of External DTD file" >

The system keyword is generally used to describe a private DTD file.

The Public keyword describes a common DTD file. Public form, which first finds the DTD file by default based on the name of the DTD, and then finds the file based on the URI of the DTD file.

The header file for the label file in JSP2 is as follows: This is the schema form used rather than the DTD file.

Copy Code code 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 under its subdirectories. In this way, Web.xml does not have to configure its location. Tomcat directly reads the TLD file 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.