struts2拦截器

1.Action级别的拦截器

   struts.xml中这样写

               <interceptors>
			<interceptor name="mySimple" class="com.action.SimpleInterceptor">
				<param name="name">我的简单拦截器</param>
			</interceptor>
		</interceptors>

                <action name="testInterceptorAction" class="com.action.TestInterceptor">
 			<interceptor-ref name="defaultStack"></interceptor-ref>
 			<interceptor-ref name="mySimple"></interceptor-ref>
 		</action>
 

    注意:action中一旦使用了自己定义的拦截器,就必须重新声明使用struts2中的默认拦截器,否则

            默认的拦截功能将不会触发。

 

    这是Interceptor类

 

package com.action;

import java.text.DateFormat;
import java.util.Date;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class SimpleInterceptor extends AbstractInterceptor {

	private static final long serialVersionUID = 6794325751895820013L;
	
	private String name ;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		String className = invocation.getAction().getClass().getName();
		System.out.println("start time : " + className + " " + DateFormat.getDateTimeInstance().format(new Date()) + " of " + name);
		String invoke = invocation.invoke();
		System.out.println("end time : " + className + " " + DateFormat.getDateTimeInstance().format(new Date())  + " of " + name);
		return invoke;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

   此类继承了AbstractInterceptor , 需要实现其中的intercept方法

 

 

   下面是Action

 

package com.action;

import com.opensymphony.xwork2.ActionSupport;

public class TestInterceptor extends ActionSupport {

	private static final long serialVersionUID = 8058442696018050533L;
	
	private String username ;
	@Override
	public String execute() throws Exception {
		System.out.println("enter action " + username);
		return null;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	
	

}
 

   当我们访问http://localhost:8080/StrutsTest/testInterceptorAction.action?username=aaa时,

   屏幕的显示如下

   start time : com.action.TestInterceptor 2010-1-3 11:50:09 of 我的简单拦截器
   enter action aaa
   end time : com.action.TestInterceptor 2010-1-3 11:50:11 of 我的简单拦截器

 

2.包拦截器

定义了包拦截器,会拦截包中的所有Action

需要定义包的默认拦截器为指定的拦截器

 

struts.xml中如下

                <interceptors>
			<interceptor name="myInterceptor" class="com.action.MyInterceptor">
				<param name="name">我的简单拦截器</param>
			</interceptor>
			
			
			<interceptor-stack name="myStack">
				<interceptor-ref name="defaultStack"></interceptor-ref>
				<interceptor-ref name="myInterceptor">
					<param name="excludeMethods">login</param>
				</interceptor-ref>
			</interceptor-stack>
			
		</interceptors>

                <default-interceptor-ref name="myStack"></default-interceptor-ref>
 

   这样就定义了一个拦截器栈,其中使用自己的myInterceptor和struts2默认的defaultStack拦截器栈,

   在自己定义的myInterceptor中,定义了不拦截的action中方法为login的方法,特别注意,这里的login指的

   是方法名是login,而不是action名为login.同样还有一个includeMethods参数,指定必须拦截的方法,

   这两个参数如果需要拦截和排除拦截的方法有多个,需要使用英文的逗号进行分割。

   使用includeMethods和excludeMethods的拦截器需要继承MethodFilterInterceptor抽象类。实现其中的

   doIntercept方法、

 

  下面是Interceptor类代码

package com.action;


import org.apache.struts2.ServletActionContext;

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

public class MyInterceptor extends MethodFilterInterceptor {

	private static final long serialVersionUID = -269084675867865274L;
	
	@Override
	protected String doIntercept(ActionInvocation innovation) throws Exception {
		Boolean login = (Boolean) ServletActionContext.getRequest().getSession().getAttribute("login");
		if(Boolean.TRUE.equals(login)) {
			return innovation.invoke();
		} else {
			return Action.LOGIN;
		}
	}

	
}

   该方法检查用户是否登陆,就是检查session中login的值是否为True.

   为了使用户能够正常登陆,在使用该拦截器时对login方法进行了排除,

   确保用户能够通过login方法将login为true添加到session中。

 

你可能感兴趣的:(apache,xml,struts)