An error occurred when dynamically displaying permissions by defining tags

Goal: Implement dynamic display permissions through custom tags

Problems: Instead of displaying the permissions the user has as expected, all permissions are displayed in the


overwritten org.apache.struts2.views.jsp.ui Class AnchorTag and added a corresponding interceptor to struts.xml, but it didn't work.


Solution :

It turned out that when querying all permissions, the requirements: all permissions are not Null and not duplicates are written as null, which leads to a logic error when judging whether it is a general permission in AnchorTag, which leads to the above Error


//The role of this class is to judge whether the user has this permission when using the label, so as to determine whether to execute the label

@Override
	public int doEndTag() throws JspException {
    	
    	//Current user
    	User user = (User) pageContext.getSession().getAttribute("user");
    	//The current URL, if there are parameters, remove the following parameter string
    	String privilegeUrl = action;
    	int pos = privilegeUrl.indexOf("?");
    	if(pos > -1){
    		privilegeUrl = privilegeUrl.substring(0,pos);
    	}
    	
    	if(user.hasPrivilegeByUrl(privilegeUrl)){
    		return super.doEndTag();//If you have permission, generate and output <a> tags normally
    	}
    	else{
    		return EVAL_PAGE;//If there is no permission, the current <a> tag will not be displayed, just continue to execute the code behind the page
    		
    	}
    }

 

public List<String> getAllPrivilegeUrls() {
		return getSession().createQuery(
				"SELECT DISTINCT p.url FROM Privilege p WHERE p.url IS NOT NULL")
				.list();
	}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327017684&siteId=291194637