Introduction to JSP custom tags

This article has shared the JSP custom tag introductory learning tutorial for you. I hope you like it.

1. JSP custom tag:

Custom tags are user-defined jsp language elements. When the JSP page contains a custom tag, it will be transformed into a servlet, and the tag will be transformed into operations on the object called tag handler, that is, those operations called by the web container when the servlet is executed. JSP tag extension allows you to create new tags and insert them directly into a JSP page. The JSP 2.0 specification introduces simple tag handlers to write these custom tags. You can inherit the simpletagsupport class and override the dotag () method to develop the simplest custom tag.

2. Develop custom labels

The following steps create a custom label for the IP address of the tactical client:

Write a common Java class that inherits the simpletagsupport class,

Create mytaglib.inf in the WEB-INF directory of the web project TLD file, which is called the declaration file of the tag library. (refer to the TLD file of the core Tag Library)

Import the custom tag library in the header of the JSP page: the URL is the URL you wrote in the TLD, and the prefix is also defined in the TLD file

Using custom tags in JSP

3. Implementation process of custom label

When accessing: http://localhost:8080/stuJsp/Hellotags.jsp Time; To restart Tomcat and start the server, load all files in the WEB-INF directory of each web application!!! For example. web. XML, TLD file!!! The steps are as follows:

4. Access tag body

You can include message content in tags like a standard tag library. If we want to include content in our custom

The format is as follows:

However, to display text, you need to modify the processing class and TLD file: modify the processing class and add the following contents to the dotag method:

Modify TLD file:

Now you can display the contents of the label body;

Output tag body content format: JSP: used in traditional tags. You can write and execute JSP java code. Scriptless: the tag body cannot write JSP java code empty: it must be an empty tag. Tagdependent: the tag body content can write JSP java code, but will not execute

5. Attribute the label body:

You can set various properties in the user-defined standard. To receive property values, the user-defined label class must implement the setter method;

The format is as follows:

The steps to define attributes are as follows: write a processing class: attributetags extensions simpletagsupport

Indicate this tag and attribute in the tag library file TLD:

Now you can use the tag with attributes. When configuring attributes in TLD, you can configure the following attributes:

6. Custom labels with sub labels:

Just like the choose tag of the core tag library, we can also define nested custom tags. In this part, we mainly explain how to create a choose tag similar to the core tag library. The steps are as follows:

Create a processing class, and the processing class still has the same method as before. It should be noted that a GetParent () method is used. From the name, we can know that it is to obtain the parent tag. Yes, it is to obtain the parent tag class; Create three processing class files: choosetag, otherwisetag and whentag

Write TLD files: as like as two peas for tags.

Using labels with labels: slightly different from using other labels, it needs to be nested

Custom tags are introduced here;

404 page, 505 page, error page configuration method:

Can be found on the web Configure the global 404 pages, 505 pages and error pages for your website in XML; The configuration method is as follows: remember to create the corresponding jump file.

JSP introduction here, I hope to help you learn.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>