Fork me on GitHub

自定义拦截器

------------------siwuxie095

   

   

   

   

   

   

   

   

自定义拦截器

   

   

1、在 Struts2 中有很多拦截器,这些拦截器是 Struts2 封装的功能,

但在实际开发中,Struts2 中的拦截器中可能没有所需要的功能,此

时,需要自己写拦截器来实现功能

   

   

   

   

2、拦截器的基本结构

   

通过查看源代码了解拦截器的基本结构

   

如:ModelDrivenInterceptor 拦截器,继承了 AbstractInterceptor 类,

在该拦截器的 intercept() 方法中写拦截逻辑

   

   

   

   

AbstractInterceptor 类实现了 Interceptor 接口

   

   

   

   

Interceptor 接口中定义了三个方法

   

   

   

   

   

   

   

3、总结:继承 AbstractInterceptor 类即可实现拦截器

   

不过,在实际开发中,建议继承 MethodFilterInterceptor

实现拦截器,它的好处是:可以不拦截 Action 中某个方法

   

   

   

   

4、拦截器 和 Action 本来没关系,如何让拦截器去拦截 Action?

   

通过配置文件的方式建立关系,让拦截器去拦截 Action

   

注意:不是在 Action 中调用拦截器的方法

   

   

   

   

   

   

   

自定义拦截器入门案例:自定义登录拦截器

   

   

1、需求

   

在项目中,有很多超链接导向不同的功能,但:

   

1)如果是登录状态,点击超链接,跳转到相应功能的页面

   

2)如果不是登录状态,点击超链接,直接返回到登录页面

   

   

   

   

2、登录状态的判断:使用 Session 域对象

   

(1)登录成功之后,把数据放到 Session 中

   

(2)根据 Session 中是否有值,判断是否为登录状态

   

   

   

   

3、实现登录的基本功能

   

查询数据库,判断用户名和密码

   

   

   

   

4、添加登录拦截器功能

   

(1)判断是否登录:判断 Session 中是否有名称是 username 的值

   

(2)拦截器实现过程

   

1)创建一个类,继承 MethodFilterInterceptor 类

   

2)重写 MethodFilterInterceptor 类中的方法,写拦截逻辑

   

   

   

   

5、 配置 Action 和 拦截器 的关系(注册拦截器

   

(1)在要拦截的 Action 所在 package 标签中声明拦截器

   

(2)在具体的 action 标签中使用已经声明的拦截器

   

   

   

   

6、注意事项:

   

1)一般 Struts2 需要执行默认拦截器,但是如果在 Action 中

配置自定义拦截器,默认拦截器就不会执行了,只需把默认拦截

器手动配置一次即可

   

   

2)配置自定义拦截器,会对 Action 中的所有方法都进行拦截

   

1)问题:在 Action 中有 login() 的登录方法,该方法不需要拦截,

如果这个方法都拦截,就永远登录不进去了

   

2)解决:在配置文件中配置不拦截 Action 中的某些方法

   

   

   

   

7、具体实现

   

1)编写页面

   

index.jsp:

   

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>首页</title>

</head>

<body>

<h1>欢迎来到首页!</h1>

<%

Object obj=request.getSession().getAttribute("username");

if(obj!=null){

%>

 

<h1><a href="${pageContext.request.contextPath }/user_logout.action">退出</a></h1>

 

<% }else{ %>

 

<h1><a href="${pageContext.request.contextPath }/login.jsp">登录</a></h1>

 

<% } %>

 

<h1><a href="${pageContext.request.contextPath }/user_list.action">列表</a></h1>

</body>

</html>

   

   

   

login.jsp:

   

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>登录页面</title>

</head>

<body>

<form action="${pageContext.request.contextPath }/user_login.action" method="post">

 

username<input type="text" name="username" />

password<input type="password" name="password" />

<input type="submit" value="提交" />

</form>

</body>

</html>

   

   

   

list.jsp:

   

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

 

<!-- 引入 Struts2 标签库 -->

<%@ taglib uri="/struts-tags" prefix="s"%>

   

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>列表</title>

</head>

<body>

   

<s:property value="username1"></s:property><br/>

<s:property value="username2"></s:property><br/>

<s:property value="username3"></s:property><br/>

<s:property value="username4"></s:property><br/>

 

</body>

</html>

   

   

   

2)编写 Action

   

UserAction.java:

   

package com.siwuxie095.action;

   

import javax.servlet.http.HttpServletRequest;

   

import org.apache.struts2.ServletActionContext;

   

import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionSupport;

import com.opensymphony.xwork2.util.ValueStack;

   

public class UserAction extends ActionSupport {

   

public String login() {

 

HttpServletRequest request=ServletActionContext.getRequest();

String username=request.getParameter("username");

String password=request.getParameter("password");

 

if (username.equals("siwuxie095")&&password.equals("8888")) {

 

//成功,向 Session 中放值

request.getSession().setAttribute("username", username);

return "succ";

 

} else {

//失败

return "err";

}

}

 

 

public String list() {

 

ActionContext context=ActionContext.getContext();

ValueStack stack=context.getValueStack();

stack.set("username1", "小赵");

stack.set("username2", "小钱");

stack.set("username3", "小孙");

stack.set("username4", "小李");

 

return "list";

}

 

 

public String logout() {

 

HttpServletRequest request=ServletActionContext.getRequest();

request.getSession().invalidate();

 

return "logout";

}

}

   

   

   

3)编写拦截器

   

LoginInterceptor.java:

   

package com.siwuxie095.interceptor;

   

import javax.servlet.http.HttpServletRequest;

   

import org.apache.struts2.ServletActionContext;

   

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

   

public class LoginInterceptor extends MethodFilterInterceptor {

   

@Override

protected String doIntercept(ActionInvocation invocation) throws Exception {

 

HttpServletRequest request=ServletActionContext.getRequest();

Object obj=request.getSession().getAttribute("username");

 

if (obj!=null) {

//是登录状态,做类似于放行操作

return invocation.invoke();

} else {

//不是登录状态,到配置文件的 result 标签处

//找名称是 err 的值,到相应路径中去

return "err";

}

 

}

 

}

   

   

   

4)配置 Action 和 拦截器

   

struts.xml:

   

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">

   

<struts>

 

<package name="demo" extends="struts-default" namespace="/">

 

<!-- 声明自定义拦截器 -->

<interceptors>

<interceptor name="loginInterceptor" class="com.siwuxie095.interceptor.LoginInterceptor"></interceptor>

</interceptors>

 

<action name="user_*" class="com.siwuxie095.action.UserAction" method="{1}">

 

<!-- 使用已经声明的自定义拦截器 -->

<interceptor-ref name="loginInterceptor">

<!-- 配置 Action 中不拦截的方法(如有多个方法,加逗号隔开) -->

<param name="excludeMethods">login</param>

</interceptor-ref>

 

<!-- 手动使用一次默认拦截器 -->

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

 

<result name="succ">/index.jsp</result>

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

<result name="list">/list.jsp</result>

<result name="logout">/index.jsp</result>

 

</action>

 

</package>

   

</struts>

   

   

   

   

   

   

   

   

   

   

【made by siwuxie095】

posted on 2017-08-15 15:13  siwuxie095  阅读(223)  评论(0编辑  收藏  举报

导航