struts2拦截器介绍

从官网上摘了两个struts2拦截器的配置方法,自己就记录下来了:

第一种方式:配置单个拦截器

<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>

第二种方式:通过拦截栈来配置拦截器

<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>

第三种方式:是引用struts2默认为我们实现的拦截器

<package name="upload" namespace="/system/upload" extends="struts-default">
		
    <action name="fileUploadOther" class="myFileUploadAction" method="fileUploadOther">
        	<!-- 动态设置savePath的属性值,设置为服务器上的路径 -->
        	<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>
        	<!-- 实现struts的默认拦截器功能,所以要在后面加一个defaultStack,默认拦截栈 -->
        	<interceptor-ref name="fileUpload">
       		<!-- 文件过滤 -->
        		<param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/jpg</param>        		    		<!-- 文件大小, 以字节为单位 -->
        		<param name="maximumSize">2097152</param>
        	</interceptor-ref>
        	<!-- 默认拦截器必须放在fileUpload之后,否则无效 -->
        	<interceptor-ref name="defaultStack" />
    </action>
        
</package>

下面是自己写的两个拦截类:

public class LoggerInterceptor extends AbstractInterceptor {

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		Map map = invocation.getInvocationContext().getSession();
		if(map.get("userId") == null){	/**用户没有登录**/
			System.out.println("没有session,重新登录。");
		}else{
			System.out.println("已有session,不用再次登录。");
		}
		return invocation.invoke();
	}

}

 

public class TimerInterceptor extends AbstractInterceptor {

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		Map map = invocation.getInvocationContext().getSession();
		if(map.get("userId") == null){	/**用户没有登录**/
			return Action.INPUT;
		}else{
			return invocation.invoke();
		}
	}

}

 

你可能感兴趣的:(struts2,struts2拦截器,struts2拦截器知识,struts2拦截器配置,struts2拦截器例子)