欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Struts2拦截器的基本用法

程序员文章站 2022-07-15 20:42:04
...
方法一:
继承抽象截器(AbstractInterceptor)
拦截器Java代码:
package action;

import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class AuthInterceptor extends AbstractInterceptor {
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();
		Object user = session.get("user");

		if (user == null) {
			ActionSupport action = (ActionSupport) invocation.getAction();
			action.addActionError("请先登录!");
			return Action.LOGIN;
		} else {
			return invocation.invoke();
		}
	}
}


Struts.xml文件:
...
<package name="Node" extends="struts-default" namespace="/Node">
	<interceptors>
		<!-- 定义AuthInterceptor拦截器 -->
		<interceptor name="auth" class="action.AuthInterceptor"/>
		<!-- 定义自己的拦截器栈,在Action的配置中,可以直接引用这个拦截器栈 -->
		<interceptor-stack name="securityStack">
			<interceptor-ref name="defaultStack"/>
			<interceptor-ref name="auth"/>
		</interceptor-stack>
	</interceptors>

	<!--<default-interceptor-ref name="securityStack"/>--> 

	<global-results>
		<result name="login">/login.jsp</result>
	</global-results>

	<action name="show" method="show" class="action.NodeAction">
		<result>show.jsp</result>
		<interceptor-ref name="securityStack"/>
	</action>

	...

</package>
...

在21行,如果直接用自己的拦截器<interceptor-ref name="authStack"/>,而不用拦截器栈时的话,默认拦截器的功能被取消了,有可能出错。
所以需要在<interceptor-ref name="authStack"/>后边加上<interceptor-ref name="defaultStack"/>这一句,再次显式的定义使用默认拦截器栈:
	<action name="show" method="show" class="action.NodeAction">
		<result>show.jsp</result>
		<interceptor-ref name="authStack"/>
		<interceptor-ref name="defaultStack"/>
	</action>



方法二:
继承方法过滤拦截器(MethodFilterInterceptor)
拦截器Java代码:
package action;

import java.util.Map;

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

public class AuthInterceptor extends MethodFilterInterceptor{
	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {
		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();
		Object user = session.get("user");

		if (user == null) {
			ActionSupport action = (ActionSupport) invocation.getAction();
			action.addActionError("请先登录!");
			return Action.LOGIN;
		} else {
			return invocation.invoke();
		}
	}
}


Struts.xml文件:
...
<package name="Node" extends="struts-default" namespace="/Node">
	<interceptors>
		<!-- 定义LoggerInterceptor拦截器 -->
		<interceptor name="logger" class="action.LoggerInterceptor">
			<param name="includeMethods"></param>
			<param name="excludeMethods"></param>
		</interceptor>
		<!-- 定义AuthInterceptor拦截器 -->
		<interceptor name="auth" class="action.AuthInterceptor">
			<param name="includeMethods">show,edit</param>
			<param name="excludeMethods">login</param>
		</interceptor>
		
		<!-- 定义自己的拦截器栈,在Action的配置中,可以直接引用这个拦截器栈 -->
		<interceptor-stack name="myStack">
			<interceptor-ref name="logger"/>
			<interceptor-ref name="auth"/>
			<interceptor-ref name="defaultStack"/>
		</interceptor-stack>
	</interceptors>
	
	<!-- 默认拦截器 -->
	<default-interceptor-ref name="myStack"/> 

	<global-results>
		<result name="login">/login.jsp</result>
	</global-results>

	<action name="show" method="show" class="action.NodeAction">
		<result>show.jsp</result>
		<!-- 自己的拦截器 -->
	</action>

	...

</package>
...

第26行,配置了默认的拦截器引用,这样在<action中就不用写<interceptor-ref了。
如果需要在<action中单独配置另外的拦截器xxxStack,可以在34行处加入:
<action name="show" method="show" class="action.NodeAction">
	<result>show.jsp</result>
	<interceptor-ref name="xxxStack"/>
	<interceptor-ref name="defaultStack"/>
</action>

引用
NOTE: If method name are available in both includeMethods and excludeMethods, it will still be considered as an included method. In short includeMethods takes precedence over excludeMethods.