Struts2---->自定义interceptor

Struts2 自定义拦截器

一、拦截器定义,一般有两种方式

1、实现Interceptor接口

public interface Interceptor extends Serializable{  
     public void init();  
     public void destroy();  
     public String intercept(ActionInvocation invocation)();  
}  

2、继承AbstractInterceptor类,重写intercept()方法即可
此方法更可行,其实AbstractInterceptor类也就是实现了Interceptor接口

invocation.invoke();表示该方法执行完后执行Action的execute()方法或者执行下一个拦截器  
invocation.getAction(); 可以将该法强制转换为Action的类类型

3、方法拦截器:继承MethodFilterInterceptor类,重写doIntercept()方法

MethodFilerInterceptor实现方法过滤中用到的两个参数

execludeMethods:该参数指定拦截器拒绝拦截的方法列表,多个方法用“,”隔开,指定了这个参数,拦截器不会拦截指定列表中的方法,就是所谓的黑名单

includeMethods:该参数指定拦截器需要拦截的方法列表,如果指定了参数,则指定的Action在执行前会被拦截,即白名单。

二、使用自定义拦截器

1、包内定义拦截器

<package....>  
     <interceptors>  
          <interceptor name="myinterceptor" class="....">  
          </interceptor>  
     </interceptors>  
</package>  

2、action内使用拦截器

<action .....>  
     <result.....></result>  
     <interceptor-ref name="defaultStack"></interceptor-ref>  
     <interceptor-ref name="myinterceptor"></interceptor-ref>  
</action>  

主要:可以看出使用了自定义拦截器的action要配置默认拦截器的引用,因为默认拦截器包含了参数的读取、session的管理等功能

三、例子代码

PermissionInterceptor.java

package com.interceptor;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
/**
 * 限制没有登录的用户进入访问页面
 */
@SuppressWarnings("serial")
public class PermissionInterceptor implements Interceptor {
    public void destroy() {
    }
    public void init() {
    	System.out.println("自定义interceptor执行");
    }
    public String intercept(ActionInvocation invocation) throws Exception {
        Object user = ActionContext.getContext().getSession().get("user");
        // 如果user不为null,代表用户已经登录,允许执行action中的方法
        if (user != null){
        	System.out.println("用户已经登录");
        	
            return invocation.invoke(); 
        }
        System.out.println("用户没有登录");
        ActionContext.getContext().put("message", "你没有权限执行该操作");
        return "input";
    }
}

TestAction.java

package com.action;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport {

	@Override
	public String execute() throws Exception {
		System.out.println("执行action");
		return super.execute();
	}

}

struts.xml

<struts>
	<constant name="struts.devMode" value="true"></constant>
	<package name="test" namespace="/" extends="struts-default">
		<interceptors>
			<interceptor name="myinterceptor" class="com.interceptor.PermissionInterceptor"></interceptor>
		</interceptors>

		<action name="test" class="com.action.TestAction">
			
			<result name="success">/success.jsp</result>
			<result name="input">/error.jsp</result>
		    <interceptor-ref name="myinterceptor"></interceptor-ref>
			<interceptor-ref name="defaultStack"></interceptor-ref>
		</action>

	</package>
</struts>

index.jsp 导航到login.jsp 和quit.jsp

 <a href="login.jsp">login.jsp</a>
    <a href="quit.jsp">quit.jsp</a>

login.jsp  向session添加user,访问action

<% request.getSession().setAttribute("user", "wsz");%>
该页面往session中添加user <a href="test.action">user.action</a>

quit.jsp   从session移除user,访问action

<%
    request.getSession().removeAttribute("user");
%>
         该页面从sessiong中移除user
    <a href="test.action">user.action</a>

success.jsp

user.action访问成功

error.jsp

 user.action访问成功

posted on 2012-09-02 22:24  小强斋太  阅读(306)  评论(0编辑  收藏  举报

导航