Summary of the implementation process of JSP custom tag taglib

Time:2021-4-18

Introduction to taglib instruction

Taglib instruction, in fact, is to define a tag library and custom tag prefix.

For example, the tag library supported in struts, HTML tag library, bean tag library and logic tag library.

We just want to introduce the specific implementation methods. We will give you a macro perspective and solve the problems. Later, we will introduce the corresponding learning materials.

In addition to the tag library of struts, we also have JSTL tag library.

In this way, the tag library or tag library file is introduced into the interface JSP, and then the tags defined in it 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 label Library

In JSP 1.1 version, a custom tag library is added. Custom tag library can be regarded as an excellent component. In the custom tag library, we can encapsulate the complex business logic functions in the tag library. You don’t have to write specific code in JSP. In this way, JSP code and Java coding capabilities do not have to be forced to couple together.

Referring to the issue of JSP version, we should first determine our JSP version, because different versions lead to different header files introduced by tag library files and different tags.

How to view the JSP version or servlet version we use

Open the JSP under the Lib file under Tomcat- api.jar / meta-inf under/ MANIFEST.MF File, view the JSP version.

Because I have imported it into Eclispe, the figure is as follows:

 

Servlet under lib file- api.jar / meta-inf under/ MANIFEST.MF File, view the servlet version

Because I have imported it into Eclispe, the figure is as follows:

New features in jsp2

In the above figure, the JSP version is 2.2 and the servlet version is 3.0. At present, servlet 3.0 corresponds to JSP 2.2 specification, but in general, we call both JSP 2.0 and JSP 2.2 JSP 2.2

If we use jsp2, then web.xml Using the version of servlet 2.4 or above. Let’s have a look web.xml Corresponding header file:

The new features are:

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://javasuncom/xml/ns/j2ee
http://javasuncom/xml/ns/j2ee/web-app_2_xsd” >
</web-app>

1. Configure the properties of JSP directly

For example, in web.xml Medium configuration:

Copy codeThe code is 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 other functions:

2. Expression language

The expression language is to use El expression.

3. Using tagfile

Tagfile can be used to process class and library files instead of tags.

The header file in the tag library file of JSP 1 and JSP 2

Header file of JSP 1 tag file:

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, we know that this is the introduction of DTD file, but what does it mean?

If DTD files are imported, DOCTYPE is used. But what does “public” / / Sun Microsystems, Inc. / / DTD JSP tag library 1.1 / / en “mean?

According to XML, how to import DTD file? DTD files can be imported in the following forms:

1. Introduce DTD into 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</heading>
<body>Don’t forget the meeting!</body>
</note>

2. Introduce external DTD file into XML document

Copy codeThe code is as follows:
<?xml version=”0″?>
<!DOCTYPE note SYSTEM “notedtd”>
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<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, use the interstitial system or public to indicate the location of the external DTD file

Use the system keyword as follows: <! The name of the DOCTYPE root element 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 a public DTD file. In the form of public, the DTD file is searched by default according to the name of the DTD file. If the DTD file cannot be found, the file will be searched according to the URI of the DTD file.

The header file of the tag file in JSP 2 is as follows: it is the schema format used 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>

Label library file location

Put the TLD file in the webcontent directory or its subdirectory. In this case, web.xml No need to configure its location. Tomcat directly reads the TLD file under webcontent.

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]