JSP custom tag / custom tag packaging

There is such a business requirement:

When editing a user, we need to set the user's role. When we go to the editing page, we need to automatically check the role that the user has selected, as shown in the following figure:

When we click Edit, we will query the user details and transfer the role set to the edit page.

Use the < C: foreach > tag to display all roles. How to check the role that the user has selected? The role is a set, and the role selected by the user is also a role. When traversing the role set, you also need to query whether there is this role in the user's role set

If you have used JS before, you can also use <% java code% >, but it is not very convenient, and <%% > this form is not unified with JSP pages and inconvenient to maintain.

Therefore, it is convenient to customize the label at this time, which is similar to < C: foreach >, and is unified with the page as a whole.

--------------------------------------------------------------Custom label--------------------------------------------------------------

To customize a label:

1. Implement simpletag interface / simpletagsupport class and override dotag() method.

2. Write the tag library descriptor (TLD) file, describe the custom tags in the TLD file and place them in the WEB-INF / directory. (if packaging is required, create a meta inf folder under Src / and put the TLD file in this folder)

3. After completing the above operations, you can import and use custom tags in JSP pages.

Take the above requirements as an example:

Step 1: customize the label class [the label can be applied not only to check boxes, but also to radio boxes, drop-down list boxes, etc.]

Step 2: write a TLD file (the TLD file can be copied from other label packages). Remember to put it under WEB-INF /,

Step 3: use in JSP page

Incoming label:

use:

--------------------------------------------------------------Custom labels: functional forms--------------------------------------------------------------

The above is the form of using labels. In the project, I also learned a way to customize labels: function form

For example, format a date of type date as a string:

Step 1: define a tool class with a static method to format the date. [it seems that it can only be static methods]

Step 2: configure the TLD file as follows:

Step 3: use. Using begindate in El expression is a date type date in request.

--------------------------------------------------------------Custom labels: packaging--------------------------------------------------------------

We can package the customized tags into a jar / war package to form our own tag library for future use.

Then, the packaging method is as follows:

1. Create a meta inf folder under Src / directory and add my Move TLD file to this folder

2. Click the project, right-click > export > select jar file > next > under Java

3. Then you can import the tag package for use

OK!

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
分享
二维码
< <上一篇
下一篇>>