Tag: struts2 Filter
1) logon interface code:
<% // Set the keyword of the session value to userrequest. getsession (). setattribute ("user", "enter"); %> the user has logged on
2) code for exiting the interface:
<% // Cancel the value set in the session, that is, the user is nullrequest. getsession (). removeattribute ("user"); %> the user has logged out.
3) Code of the interceptor class permissioninterceptor:
Public class permissioninterceptor implements interceptor {public void destroy () {// call upon destruction} public void Init () {// call when enabled} // This method returns the view name Public String intercept (actioninvocation Invocation) throws exception {// get the User Content Object User = actioncontext. getcontext (). getsession (). get ("user"); // if the user is not null, it indicates that the user has logged on. actionif (user! = NULL) {return invocation. invoke (); // call Action} // if the user is empty, no action is called, and a string is saved to the session. "You are not authorized to perform this operation" actioncontext. getcontext (). put ("message", "You are not authorized to perform this operation"); Return "success ";}}
3) struts. xml file Configuration:
<Struts> <package name = "packagename" namespace = "/test" extends = "struts-Default"> <! -- Define an interceptor group --> <interceptors> <interceptor name = "permission" class = "permissioninterceptor. permissioninterceptor"/> <! -- Custom interceptor Stack --> <Interceptor-stack name = "permissionstack"> <! -- System Custom interceptor stack, introduced before custom interceptor --> <Interceptor-ref name = "defaultstack"/> <! -- Introduce a custom interceptor --> <Interceptor-ref name = "permission"/> </Interceptor-stack> </interceptors> <! -- Define the default global interceptor <default-interceptor-ref name = "permissionstack"/> --> <! -- Define Global View --> <global-Results> <result name = "success">/index. JSP </result> </Global-Results> <action name = "hello *" class = "interceptor. interceptexception "method =" {1} "> <! -- Manually call the system Interceptor. When the interceptor is manually called, the default system interceptor defined will not be used --> <Interceptor-ref name = "permissionstack"/> <! -- When a custom interceptor is called, only the system interceptor is called, first, use the same custom interceptor and system interceptor --> <Interceptor-ref name = "interceptorname"/> </Action> </package> </struts>
Implementation of struts2_11 _ custom interceptor