struts拦截器使用方法

来源:互联网 发布:mac如何使桌面图标 编辑:程序博客网 时间:2024/05/16 19:53

     拦截器(Interceptor)拦截器是Struts2的核心,Struts2的众多功能都是通过拦截器来实现的。拦截器和之前我们jsp中的filter原理相同,使用方法也类似。

1.测试拦截器内部方法的先后执行顺序

  TheInterceptor1.java(也可继承AbstractIntercepter父类):

  public class TheInterceptor1 implements Interceptor

{

public void destroy()

{

// TODO Auto-generated method stub

}

public void init()

{

-

System.out.println("init invoke");

}

public String intercept(ActionInvocation invocation) throws Exception

{

System.out.println("before");

String result = invocation.invoke();

System.out.println("after");

return result;

}

}

  struts.xml:

           <interceptors>

<interceptor name="theInterceptor" class="com.erzong.interceptor.TheInterceptor1"></interceptor>

</interceptors> 

  在需要使用拦截器的action的result后面声明所使用的拦截器的名字

  执行结果:

                


          

2  一旦定义了自己的拦截器,将其配置到 action上后,我们需要在action的最后加上默认的拦截器栈:defaultStack。

  如:

     

3.在初始化拦截器时候添加初始化信息

  struts.xml:

  <interceptors>

<interceptor name="theInterceptor" class="com.erzong.interceptor.TheInterceptor1">

<param name="test">erzong</param>

</interceptor>

</interceptors>

 再在拦截器里定义名字为test的变量,并为其添加set和get方法,当拦截器启动时,就会自动找到这个属性并附上值。

4.多个拦截器累加形成的执行顺序:先从先配置进去的,再分别调用两个拦截器的的Invocation.invoke()方法,最后先从后配置的拦截器出来,再从先配置的拦截器出来。

  

5.Invocation.getAction()方法:

  得到当前拦截器进行拦截的action类

6.拦截器拦截的是目标action的execute()方法,但是前面我们所讲的可以用自定义的(例如myExecute方法)替代execute,所以为了让拦截器拦截自己的方法,必须继承MethodFilterInterceptor类。

  (1)拦截器继承MethodFilterIntercepter类,并实现doIntercept();

   (2)

 

   includeMethods:要进行过滤的方法

   excludeMethods:不进行过滤的方法

   如果一个方法在两个参数都有设定,那么则设定该方法为includeMethods

  (3)在方法过滤拦截器中,如果既没有指定 includeMethods 参数,也没有指定execludeMethods 参数,那么所有的方法都会被拦截,也就是说所有的方法都被认为是 includeMethods 的;如果仅仅指定了 includeMethods,那么只会拦截 includeMethods 中的方法,没有包含在includeMethods 中的方法就不会被拦截。

7.拦截器在登录验证的时候使用方式,当用户登录时候,如果没有登录,则会转到登录界面,如果登录成功了,则会转到下一个页面。

LoginInterceptor:

public class LoginInterceptor extends AbstractInterceptor

{

@Override

@SuppressWarnings("unchecked")

public String intercept(ActionInvocation invocation) throws Exception

{

if(LoginAction.class == invocation.getAction().getClass())//排除掉LoginAction

{

return invocation.invoke();

}

Map map = invocation.getInvocationContext().getSession();//获得map形式的httpsession

if(null == map.get("userInfo"))

{

return Action.LOGIN;//回到登陆界面

}

return invocation.invoke();//什么都不处理,继续往下走

}

}

action中execute方法:

  if(loginService.isLogin(usernamepassword))

{

User user = new User();

user.setUsername(username);

user.setPassword(password);

ActionContext.getContext().getSession().put("userInfo", user);

return SUCCESS;

}

return INPUT;

struts.xml:(为了使这个拦截机器可以拦截所有action,又不想在action一个个配置,可以先配置拦截器栈,再定义自己的默认拦截器)

   <interceptor-stack name="myDefaultInterceptorStack">

<interceptor-ref name="loginInterceptor"></interceptor-ref>

<interceptor-ref name="defaultStack"></interceptor-ref>

   </interceptor-stack>

  

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

  <global-results>

<result name="login">/error.jsp</result>

</global-results>

8.如果需要定义多个xml配置文件,则可以把这些配置文件放在src目录下,然后在struts.xml中添加例如

原创粉丝点击