Struts 2 method of realizing permission control through custom tags

Recently, I encountered a demand in development: whether to display an operation button according to the user's permission.

For example, if the user has the permission to delete data, the delete button will be displayed in the interface; If the user does not have this permission, the corresponding button will not be displayed in the interface.

In this way, you need to use custom labels.

To define a custom tag for struts 2, you need only three steps:

1. Define a component class and inherit it from org apache. struts2. components. Component;

2. Define a tag class and inherit from import org apache. struts2. views. jsp. ComponentTagSupport;

3. Create the corresponding Ltd file in the WEB-INF directory

Let's implement it one by one:

Step 1: define component class

Component, as the name suggests, is "component". This encapsulates the processing logic required by the tag.

The component class we define needs to inherit org apache. struts2. components. Component;。 In the parent class, there are two important methods: the start () method and the end () method. These two methods correspond to the start tag and the end tag respectively. We can operate on the tag through these two methods.

In addition, if the tag needs attributes, the corresponding attributes should be defined in this class, and the corresponding get () and set () methods should be provided to encapsulate these attributes.

Then, we can process the tags and attributes.

Here is my code. The function is to compare the URL address of each button to be displayed with the URL corresponding to the user's permission. If the user has permission to access the URL, the button will be displayed, otherwise nothing will be displayed.

Step 2: define tag class

Tag class, to put it bluntly, is the tag we use.

To create a tag class, you first need to inherit org apache. struts2. views. jsp. Componenttagsupport class.

Then, override the getBean () method and the populateparams () method.

The getBean () method is used to return an instance of the tag component, that is, the component created in step 1.

The populateparams () method is used to populate the attributes, that is, encapsulate the attributes in our tag into the tag class. Therefore, we also need to define the corresponding properties in the tag class and provide get () and set () methods.

In this way, we can specify the component to process the label.

Some people may have a question: why don't we write the tag processing logic directly in the tag class, but define a component class separately?

This is because: struts 2 supports a variety of presentation layer technologies. Using component can use different processing methods for different presentation layer technologies to improve the scalability of the program.

Give my code:

The function of this code is to use the component defined in step 1 to process the label.

Step3: create a TLD file in the WEB-INF directory

The TLD file describes the syntax of the tag, such as what attributes the tag has, whether the attributes of the tag support expressions, etc.

Before we use tags in JSP, we need to introduce corresponding tag libraries, so we need to define TLD files to enable our custom tags to be used in JSP.

Give the TLD file I defined, which has several simple properties.

Of which:

Short name: equivalent to prefix

Uri: the URI in <% @ taglib >

Tag: a tag

Tag class: tag class, which is our tag class

Body content: the content that can be written in the tag body

Attribute: attribute

Name: attribute name

Required: required

Rtexprvalue: whether expressions are supported

There are many contents in TLD files. More contents can be obtained by Google. I won't repeat them here.

At this point, we can use custom tags in JSP.

How to use:

1. Import tag library: <% @ taglib prefix = "OA" URI = "/ WEB-INF / oatag. TLD"% >

2. Use tag: < OA: a actionurl = "user_editui? Id = ${ID}" value = "modify" / >

In this way, when the user views the current page, he can select whether to display the operation button according to the user's permission.

summary

The above is the introduction of the method of realizing permission control through user-defined tags in struts 2. I hope it will be helpful to you. Interested friends can refer to: solutions to the problem that struts and servlet cannot coexist. Struts 2 modifies the upload file size limit, parses the struts 2 development process and detailed configuration, etc. If you have any questions, you can leave a message. Xiaobian will reply to you in time.

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