The permission system is implemented through the struts2 Interceptor. There are too many examples on the Internet.
Only the control on whether a connection can be displayed on the page is recorded here.
Scenario: the user does not have the permission to perform the "Modify record" operation. Therefore, the "Modify record" link should not appear on the page.
Environment: the version of struts2.1.8 does not matter.
My actual environment: eclipse3.5 + JBoss tool + ejb3 + mysql5
Steps:
1. Create a TLD file under the WEB-INF, I named it here ems-tag.tld. The content is copied from the Struts-core.jar/meta-INF/directory
" permission control UI tags "
1.0
EMS
/ems2-tags
A
CN. vs9. EMS. web. tags. UI. anchortag
JSP
The following
Code is skipped, exactly the same as struts2 ....
2. inherit the tag class: org. Apache. struts2.views. jsp. UI. anchortag
/** Rewritten struts2 link label
* <EMS: A action = "$ {actionurl}"> link </EMS: A>
*
* Label: determines whether the "permission" is available before executing the URL to display the execution link on the page.
* "Permission" depends on whether the user group has the permission to execute this action
*/
Public Class Anchortag Extends Org. Apache. struts2.views. jsp. UI. anchortag {
/**
* Whether authorization is passed. No by default
*/
Private Boolean Isaccess = False ;
@ Override
Public Int Doendtag () Throws Jspexception {
System. Out. println ( " Doendtag " );
If ( ! Isaccess ){
Return Skip_body;
}
Return Super . Doendtag ();
}
@ Override
Public Int Dostarttag () Throws Jspexception {
System. Out. println ( This . Action ); // Determine Based on URL
// Replace this with the specific permission judgment
Isaccess = True ;
If ( ! Isaccess ){
Return Skip_body;
}
Return Super . Dostarttag ();
}
}
3. Try it on the page
Run now,Try to switch the isaccess in the label class=True | FalseSee the results
Other labels can also be used. .
Currently, I have implemented a tag and action tag. , The project is enough.
Ideas :
Pagination of pages into multiple views .
Pass < EMS : Action name = " User_list " Executeresult = " True " Namespace = " /JSON " /> Method . Dynamic combination of page Components
<% @ Page Language = " Java " Contenttype = " Text/html; charsets = UTF-8 " Pageencoding = " UTF-8 " %>
<% @ Taglib prefix = " S " Uri = " /Struts-tags " %>
<% @ Taglib prefix = " EMS " Uri = " Ems2-tags " %>
< EMS : A action = " /Data/user_update.action? Entity. ID = 1 " > Modify user </ EMS : A >