struts2自定义拦截器
拦截器生命周期:
随着程序的开始而创建,随着程序的结束而销毁
(不可能每个访问都创建一个拦截器)
方式一
实现Interceptor接口
public class MyInterceptor1 implements Interceptor{
// 生命周期方法
// 拦截器初始化方法
@Override
public void init() {
}
// 拦截方法
@Override
public String intercept(ActionInvocation arg0) throws Exception {
return null;
}
// 销毁方法
@Override
public void destroy() {
}
}
方式二
继承AbstractInterceptor
public class MyInterceptor2 extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation arg0) throws Exception {
return null;
}
}
方式三(使用这种)
继承MethodFilterInterceptor(方法过滤拦截器)
public class MyInterceptor3 extends MethodFilterInterceptor{
// 拦截方法
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
// 前处理
System.out.println("前处理");
<!--放行方法-->
<!--所有拦截器都执行完毕后 会拿到一个字符串-->
<!--这个字符串就是action类 执行完毕后返回的字符串-->
String result = invocation.invoke();
// 后处理
System.out.println("后处理");
return result;
}
}
Action类
public class Demo01Action extends ActionSupport{
public String add() {
System.out.println("add");
return "success";
}
public String delete() {
System.out.println("delete");
return "success";
}
public String update() {
System.out.println("update");
return "success";
}
public String find() {
System.out.println("find");
return "success";
}
}
struts.xml
配置自定义拦截器(配置信息参考struts-default.xml)
<struts>
<package name="inter" namespace="/" extends="struts-default">
<!-- 1.注册拦截器 -->
<interceptors>
<!-- 注册自己定义的拦截器 -->
<interceptor name="MyInterceptor3" class="com.lanou3g.intercept.MyInterceptor3"></interceptor>
<!-- 2.注册拦截器栈 -->
<interceptor-stack name="myStack">
<!-- 引入拦截器 -->
<interceptor-ref name="MyInterceptor3">
<!--
设置不想拦截的方法 逗号隔开多个方法
includeMethods表示设置想拦截的方法
-->
<param name="excludeMethods">add,update</param>
</interceptor-ref>
<!-- 除了引用自己的拦截器 还要使用系统默认拦截器 -->
<!--
建议:先放自己的拦截器 再放系统的拦截器
自己写的拦截器 可能会用到系统的拦截器封装好的功能
-->
<!-- 引入系统默认的拦截器栈 -->
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 3.设置默认拦截器栈是哪个 -->
<default-interceptor-ref name="myStack"></default-interceptor-ref>
<action name="Demo01Action_*" class="com.lanou3g.intercept.Demo01Action" method="{1}">
<result name="success">/hello.jsp</result>
</action>
</package>
</struts>
自定义拦截器实现登录检查
创建自定义拦截器
public class MyInterceptor extends MethodFilterInterceptor{
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
// 根据session域中 是否存放了User对象
// 来判断 用户是否登录了
Object object = ActionContext.getContext().getSession().get("user");
if (object == null) {
// 没登录 重定向到登录页面
return "toLogin";
} else {
// 登录了放行
return invocation.invoke();
}
}
}
Action类
public class UserAction extends ActionSupport implements ModelDriven<User>{
// 声明一个service
private UserService service = new UserServiceImpl();
// 使用模型驱动
private User u = new User();
// 声明登录的action方法
public String login() {
System.out.println(u);
// 调用service方法
User findU = service.login(u);
// 保存到session域中(登录状态)
ActionContext.getContext().getSession().put("user", findU);
// 表示重定向回首页
return "toIndex";
}
@Override
public User getModel() {
return u;
}
}
service层
<!--
只根据名字查询用户 如果查询出用户(这里包含着用户所有信息)
再比对用户的密码是否正确
-->
if (findU == null) {
// 没有这个用户 使用异常处理
throw new RuntimeException("用户不存在");
}
if (!findU.getUser_password().equals(u.getUser_password())) {
// 密码不正确
throw new RuntimeException("密码不正确");
}
// 返回查询的对象
return findU;
login.jsp
<!-- 引入struts标签库 -->
<%@ taglib uri="/struts-tags" prefix="s" %>
<!-- 显示错误信息 -->
<!-- 获取异常信息 message
相当于调用了getMessage()方法 -->
<font color="red">
<s:property value="exception.message"/>
</font>
<!-- 查看值栈标签 -->
<s:debug></s:debug>
struts.xml配置
<struts>
<package name="web" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="MyInterceptor" class="com.lanou3g.web.MyInterceptor"></interceptor>
<interceptor-stack name="myStack">
<interceptor-ref name="MyInterceptor">
<!-- 设置不拦截登录方法 -->
<param name="excludeMethods">login</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack"></default-interceptor-ref>
<!-- 设置全局的拦截器返回结果 进行跳转设置 -->
<global-results>
<result name="toLogin" type="redirect">/login.jsp</result>
</global-results>
<!-- 配置全局异常处理页面 异常的映射 -->
<global-exception-mappings>
<!-- exception 异常错误类型(全类名) -->
<!-- result 出现异常返回的结果字符串 -->
<!-- action 可以根据这个 异常的结果 跳转一个页面 -->
<exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping>
</global-exception-mappings>
<action name="UserAction_*" class="com.lanou3g.web.UserAction" method="{1}">
<!-- 登录成功后 重定向回首页 -->
<result name="toIndex" type="redirect">/index.htm</result>
<!-- 配置一个异常处理 要跳转的页面 -->
<result name="error">/login.jsp</result>
</action>
</package>
</struts>