Custom tags for control of site permissions

Source: Internet
Author: User
Tags tld

In the writing of HTML documents, some functions with existing tags can not be completed or with the existing tags can not be very simple to meet the user requirements, it is necessary for the programmer to write their own corresponding tags to complete the specified function
Here the author of the control of the site permissions to begin to explain the implementation of the custom label and its benefits, I hope to like to build a station for the readers have some help
In the process of website construction, I believe many developers will worry about the control of permissions, although in theory, it is feasible to create a set of pages for each role, but this is not acceptable in actual operation, because there may be a small difference between the functions of the two roles, but to create two sets of pages, This extra workload and time overhead is not tolerated by the development, and most importantly, the maintenance work is difficult to carry out later, so it is important to develop a set of interface pages that are common to different roles.
So now the requirement is to achieve universal, but also allows different users to see different views (note here The view Display control mechanism and MVC control mechanism is different, MVC control is controlled by C to different pages to achieve different views of the display, And here is the content of the same page according to the user's role to display, which is not the same, perhaps most readers first think of the method is to add a large number of if else statements in the page, to determine the user identity, and then to display the content of the page, of course, this is a method, However, this method is very cumbersome to operate, and when my authorization verification mechanism has changed, then I have to modify all the corresponding code in the page, update maintenance is quite troublesome
Here the author through the form of custom tags to complete the permissions and page display content control, the use of more flexible, and maintenance is also convenient, the specific implementation of the following way

Step one, create a new TLD file, This is named Privilege.tld, this file mainly implements the definition of the custom label, note that the red part of the tag, the reader can modify the value, and the blue part of the label is the process of the class of the tag is the path and its name, the reader can modify the value according to the actual,OperateidRepresents the ID of the operation,RoleidRepresents the user's role number,MarkRepresents a mask that is used to mask a part of a role, and the contents of the file are as follows
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE taglib Public "-//sun Microsystems, Inc.//dtd JSP Tag Library 1.1//en" "http://java.sun.com/j2ee/dtds/ Web-jsptaglibrary_1_1.dtd ">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>Privilege</shortname>
<tag>
<name>operate</name>
<tagclass>Com.tag.pub.PrivilegeTag</tagclass>
<bodycontent>JSP</bodycontent> <!--here if set to empty, no body-->
<attribute>
<name>Operateid</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>Roleid</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>Mark</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
Step two, create a new label processing class, here I was built in src/com/tag/pubPackage, and the file name isPrivilegetag.java,Note that the part of the blue callout is consistent with the blue callout in the code above, otherwise an error occurs, and then a red callout in the file, according to the Operateid,roleid,mark (these three parameters do not require the user to manually call the function to set, The reflection mechanism of Java can call the corresponding set method to set the parameter value of the parameter Chaku to determine whether the user has permission to do the current operation, the contents of the file are as follows:
Package com.tag.pub;

Import javax.servlet.jsp.JspException;
Import Javax.servlet.jsp.tagext.TagSupport;

public class Privilegetag extends TagSupport {
Private static final long serialversionuid = -532517444654109642l;
Private String Operateid;
Private String Roleid;
Private String Mark;
public void Setmark (String mark) {
This.mark = Mark;
}
public void Setroleid (String roleid) {
This.roleid = Roleid;
}
public void Setoperateid (String operateid) {
This.operateid = Operateid;
}
public int doStartTag () throws Jspexception {
Ifto judge the authority here.)
return eval_page;
return skip_body;
}
}
Step Three,for link operations that can be displayed to a specified user in a page and require permission control, you should use<privilege:operate operateid= "Operid" roleid= "Roleid" > </privilege:operate> tag pairs included, For people with different identities to access the page, there will be different display purposes, in the label Operateid follows the following specifications: module name _ operation, The Roleid value is the user's role. As I am on a view page aaa_view.jsp , the actions that may be presented to the user are add, del, Fix Three action, I can render the link to the user in the following ways:

<privilege:operate operateid= "Aaa_add" roleid= "Roleid" > add </privilege:operate>

<privilege:operate operateid= "Aaa_del" roleid= "Roleid" > delete </privilege:operate>

<privilege:operate operateid= "Aaa_fix" roleid= "Roleid" > Modify </privilege:operate>

that is, when writing code regardless of whether the user has this permission, and only consider that the operation of the page requires permission control, so that different users in the access, you will see different features, such as a user has added and deleted features, then he visited the page when the corresponding place will be displayed: Add Delete the link, and for the second user, only the modified permission, then he visited the page when the corresponding place will be displayed: The modified link, the other does not have access to the operation of the link will not be displayed or executed.

The last to explain is that this is just to provide you with a way of thinking, because the specific operation is also required in the background database and table support, the upload is not supported here, and if all the code is pasted over the obvious is not realistic, so the reader can be based on this idea of their own design, If there is any problem when the implementation, you can give me a message and leave the contact information, I can send the implementation of the project documents to you

This digest from: http://blog.sina.com.cn/s/blog_7b62c61c0100vn3e.html

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.