1. 编写拦截器,需要实现Interceptor接口,实现接口中的三个方法
protected String doIntercept(ActionInvocation invocation) throws Exception {
// 获取session对象
User user = (User) ServletActionContext.getRequest().getSession().getAttribute("existUser");
if(user == null){
// 说明,没有登录,后面就不会执行了
return "login";
}
return invocation.invoke();
}
2. 需要在struts.xml中进行拦截器的配置,配置一共有两种方式
<!-- 定义了拦截器 第一种方式
<interceptors>
<interceptor name="DemoInterceptor" class="com.itheima.interceptor.DemoInterceptor"/>
</interceptors>
-->
<!-- 第二种方式:定义拦截器栈 -->
<interceptors>
<interceptor name="DemoInterceptor" class="com.itheima.interceptor.DemoInterceptor"/>
<!-- 定义拦截器栈 -->
<interceptor-stack name="myStack">
<interceptor-ref name="DemoInterceptor"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<action name="userAction" class="com.itheima.demo3.UserAction">
<!-- 只要是引用自己的拦截器,默认栈的拦截器就不执行了,必须要手动引入默认栈
<interceptor-ref name="DemoInterceptor"/>
<interceptor-ref name="defaultStack"/>
-->
<!-- 引入拦截器栈就OK -->
<interceptor-ref name="myStack"/>
</action>
一、方式一
1.1 代码
拦截器的代码:
package com.ken.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
/**
* 编写简单的拦截器
*
*/
public class DemeIncerceptor extends AbstractInterceptor {
private static final long serialVersionUID = 6410603239475356232L;
/**
* intercept用来拦截的
*/
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("Action方法执行之前...");
// 执行下一个拦截器 拦截器
String result = invocation.invoke();
System.out.println("Action方法执行之后...");
return result;
}
}
目标Action
package com.ken.action3;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("我是Action, 我正常执行...");
return NONE;
}
}
1.2 配置
<package name="demo3" namespace="/" extends="struts-default">
<interceptors>
<!-- 定义了拦截器 -->
<interceptor name="demeIncerceptor" class="com.ken.interceptor.DemeIncerceptor" />
</interceptors>
<action name="userAction" class="com.ken.action3.UserAction">
<!-- 只要是引用了自己的拦截器,默认栈的拦截器就不执行了,必须要手动引入默认栈 -->
<interceptor-ref name="demeIncerceptor" />
<interceptor-ref name="defaultStack" />
</action>
</package>
1.3 运行效果
二、方式二
2.1 代码
同上
2.2 配置
<package name="demo3" namespace="/" extends="struts-default">
<!-- 定义了拦截器
<interceptors>
<interceptor name="demeIncerceptor" class="com.ken.interceptor.DemeIncerceptor" />
</interceptors>
-->
<!--第二种方式:定义拦截器栈 -->
<interceptors>
<interceptor name="demeIncerceptor" class="com.ken.interceptor.DemeIncerceptor"/>
<!-- 定义拦截器栈 -->
<interceptor-stack name="myStack">
<interceptor-ref name="demeIncerceptor"/>
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<action name="userAction" class="com.ken.action3.UserAction">
<!-- 只要是引用了自己的拦截器,默认栈的拦截器就不执行了,必须要手动引入默认栈
<interceptor-ref name="demeIncerceptor" />
<interceptor-ref name="defaultStack" />
-->
<!-- 引入拦截器栈 -->
<interceptor-ref name="myStack"/>
</action>
</package>
2.3 运行效果
同上
三、例子
需求:访问方法的时候都拦截一下,看看是不是已经登录。
我们可以拦截所有方法,除了登录方法。由于AbstractInterceptor会拦截所有方法,所以,我们选择MethodFilterInterceptor,它里面有excludeMethods
和includeMethods
。
3.1 拦截器编写
package com.ken.interceptor;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class UserInterceptor extends MethodFilterInterceptor {
private static final long serialVersionUID = -597288551062770495L;
/**
* 进行拦截的方法
*/
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
// 获得session对象
User user = ServletActionContext.getRequest().getSession().getAttribute("existUser");
if (user == null) {
// 说明,没有登录
return "login";
}
// 放行
return invocation.invoke();
}
}
3.2 配置全局跳转页面
<global-results>标签配置全局的跳转页面
3.3 配置interceptor
<interceptors>
<interceptor name="userInterceptor" class="com.ken.interceptor.UserInterceptor"/>
</interceptors>
<global-results>
<result name="login">/login.html</result>
</global-results>
<!-- 配置Action -->
<action name="hello" class="com.ken.action.HelloAction" method="sayHello">
<!-- 配置跳转的页面,路径的写法:在struts2框架中,不管是转发还是重定向都不用写项目名 -->
<!-- name中的ok叫做逻辑视图名称 -->
<result name="ok">/demo1/success.jsp</result>
<interceptor-ref name="userInterceptor">
<param name="excludeMethods">login</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
</action>
这里的关键是interceptory-ref里面的excludeMethods。