`

Struts2拦截器

阅读更多

Struts2添加拦截器

1.struts.xml的配置

<!-- 后台拦截器 -->
	<package name="authpkg" extends="struts-default" namespace="">
		<interceptors>
			<!-- 定义拦截器 -->
			<interceptor name="authority" class="com.syxp.yjxx.core.auth.AuthorityInterceptor" />
			<!-- 定义拦截器栈 -->
			<interceptor-stack name="authStack">
				<!-- 将拦截器加入默认的拦截器栈中 -->
				<interceptor-ref name="authority">
					<!-- 定义不使用拦截器的方法 -->
					<param name="excludeMethods">login</param>
				</interceptor-ref>
				<interceptor-ref name="defaultStack"></interceptor-ref>
			</interceptor-stack>
		</interceptors>
		<default-interceptor-ref name="authStack" />
		<!-- 定义拦截器的全局变量,跳转路径 -->
		<global-results>
			<result name="login" type="redirect">/index.jsp</result>
		</global-results>
	</package>

 2. 拦截器类com.syxp.yjxx.core.auth.AuthorityInterceptor

/**
 * AuthorityInterceptor.java
 * 
 * 功能:登录验证拦截器
 * 类名:AuthorityInterceptor
 * 
 *   ver     			变更日                    	部门               	开发者     			变更内容
 * ─────────────────────────────────────────────────────
 *   V1.00  		2013-11-07          	研发部	         常宝龙          		初版
 *   
 *   Copyright (c) 2008, 2013 Infopower corporation All Rights Reserved.
 */
package com.syxp.yjxx.core.auth;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

/**
 * 登录验证拦截器
 * 
 * @author 常宝龙
 * @version Ver 1.0 2013-11-07 新建
 * @since CodingExample Ver 1.0
 * 
 */
public class AuthorityInterceptor extends MethodFilterInterceptor{
	
	private static final long serialVersionUID = 3457293729347L;

	@Override
	protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
		Object user = actionInvocation.getInvocationContext().getSession().get("user2");
		if(user!=null){
			//递归调用拦截器
			return actionInvocation.invoke();
		}else{
			return Action.LOGIN;
		}
	}
}

 3.xml文件中配置好后,需要在其他的action中调用这个拦截器,调用方法如下:

<!-- 报表图(辽宁电网受暴雨影响情况) -->
	<package name="stormReport" namespace="/stormReport" extends="authpkg">
</package>

 在extends选择集成拦截器的包

1
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics