自定义拦截器

来源:互联网 发布:电脑电音软件 编辑:程序博客网 时间:2024/06/03 22:04

------------------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>

<metahttp-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><ahref="${pageContext.request.contextPath }/user_logout.action">退出</a></h1>

 

<% }else{%>

 

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

 

<% }%>

 

<h1><ahref="${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>

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

<title>登录页面</title>

</head>

<body>

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

 

username<inputtype="text"name="username"/>

password<inputtype="password"name="password"/>

<inputtype="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>

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

<title>列表</title>

</head>

<body>

  

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

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

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

<s:propertyvalue="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 UserActionextends 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 LoginInterceptorextends 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:

  

<?xmlversion="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>

 

<packagename="demo"extends="struts-default"namespace="/">

 

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

<interceptors>

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

</interceptors>

 

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

 

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

<interceptor-refname="loginInterceptor">

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

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

</interceptor-ref>

 

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

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

 

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

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

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

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

 

</action>

 

</package>

  

</struts>

  

  

  

  

  

  

  

  

  

  

【made by siwuxie095】