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

struts2拦截器

程序员文章站 2022-03-15 15:03:14
...

Struts2提供了强大的拦截器功能,特别是加上convention plugin之后,使用更方便:

配置拦截器:

<package name="my-default" extends="convention-default">
          <interceptors>
		<interceptor name="checkLogin" class="com.abc.interceptor.CheckLoginInterceptor"/>

		<interceptor-stack name="checkLoginstack">
                        <interceptor-ref name="checkLogin"></interceptor-ref>
                        <interceptor-ref name="defaultStack"></interceptor-ref><!--这里必须引用Struts2默认的拦截器defaultStack,否则会使传递的url参数丢失-->
                </interceptor-stack>
	</interceptors>

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


</package>

  简单的拦截器

package com.abc.interceptor;

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

/**
 * 
 * @author Wilson
 *
 */
public class CheckLoginInterceptor extends AbstractInterceptor {

	private static final long serialVersionUID = 1L;
	

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		......................
		String result = invocation.invoke();
		
		return result;
	}


}

 在action使用

@InterceptorRef("checkLoginstack")//全局引用拦截器
public class UserAction extends ActionSupport<Object> {
        ......

//局部引用拦截器,引用多个拦截器的话则是nterceptorRefs({@InterceptorRef("interceptor-1"),@InterceptorRef("interceptor-2")})
@Action(value = "/input", results = { @Result(name = "INPUT", location = "/input.jsp"),
			@Result(name = "login", location = "/login.jsp") },interceptorRefs = { @InterceptorRef("checkLoginstack") })
	public String input() throws Exception {
        ...........
    
        return "INPUT";
        }

}
相关标签: JSP