Introduction to struts2 interceptor

Source: Internet
Author: User

I picked two struts2 interceptor configuration methods from the official website and recorded them myself:

Method 1: configure a single interceptor

<package name="default" namespace="/system/login" extends="struts-default">   <interceptors>       <interceptor name="timer" class="com.zyujie.common.TimerInterceptor"/>       <interceptor name="logger" class="com.zyujie.common.LoggerInterceptor"/>   </interceptors>   <action name="login" class="userLogin" method="login">        <interceptor-ref name="timer"/>        <interceptor-ref name="logger"/>        <result name="input">login.jsp</result>        <result name="success" type="redirectAction">/secure/home</result>   </action></package>

Method 2: configure the interceptor through the interception Stack

<package name="default" namespace="/system/login" extends="struts-default">   <interceptors>        <interceptor name="timer" class="com.zyujie.common.TimerInterceptor"/>        <interceptor name="logger" class="com.zyujie.common.LoggerInterceptor"/>        <interceptor-stack name="myStack">           <interceptor-ref name="timer"/>           <interceptor-ref name="logger"/>        </interceptor-stack>    </interceptors>    <action name="login" class="userLogin" method="login">         <interceptor-ref name="myStack"/>         <result name="input">login.jsp</result>         <result name="success" type="redirectAction">/secure/home</result>    </action></package>

The third method is to reference struts2 as our default interceptor.

<Package name = "Upload" namespace = "/system/upload" extends = "struts-Default"> <action name = "fileuploadother" class = "myfileuploadaction" method = "fileuploadother"> <! -- Dynamically set the savepath attribute value to the path on the server --> <Param name = "savepath"> D: \ webapp \ apache-Tomcat-6.0.10 \ webapps \ struts2upload \ upfiles </param> <Param name = "fileopfilename"> abcde.xls </param> <result name = "success" type = "Redirect">/OK. JSP </result> <result name = "input" type = "Redirect">/index. JSP </result> <! -- Implement the default interceptor function of Struts. Therefore, a defaultstack should be added to the backend to interceptor. --> <Interceptor-ref name = "fileupload"> <! -- File filtering --> <Param name = "allowedtypes"> image/BMP, image/PNG, image/GIF, image/JPEG, image/jpg </param> <! -- File size, in bytes --> <Param name = "maximumsize"> 2097152 </param> </Interceptor-ref> <! -- The default interceptor must be placed after fileupload; otherwise, it is invalid --> <Interceptor-ref name = "defaultstack"/> </Action> </package>

The following are two interception classes written by myself:

Public class loggerinterceptor extends actinterceptor {@ overridepublic string intercept (actioninvocation Invocation) throws exception {map = invocation. getinvocationcontext (). getsession (); If (map. get ("userid") = NULL) {/** user not logged on **/system. out. println ("No session, log on again. ");} Else {system. Out. println (" the session already exists. You do not need to log on again. ") ;}Return invocation. Invoke ();}}

 

Public class timerinterceptor extends actinterceptor {@ overridepublic string intercept (actioninvocation Invocation) throws exception {map = invocation. getinvocationcontext (). getsession (); If (map. get ("userid") = NULL) {/** user not logged on **/return action. input;} else {return invocation. invoke ();}}}

 

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.