Implementation of struts2_11 _ custom interceptor

Source: Internet
Author: User

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

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.