您的位置:首页 > 编程语言 > Java开发

struts2自定义拦截器

2015-10-28 14:51 323 查看
问题?struts2自定义拦截器的作用是什么?在我的另外一篇servlet文章中提到的。在用户登录的时候回用到session啦来给标记,但是在三大框架的过程中没有servet,那该怎么办呢?

这个时候我们就需要自定义拦截器来做这个登录事情了。

在此做这个之前要把struts2的环境搭好喔!详情:http://blog.csdn.net/mr_li13/article/details/49391329

关键包:xwork2.jar

一、自定义拦截器

1、编写一个类,实现com.opensymphony.xwork2.interceptor.Interceptor

2、主要实现public String intercept(ActionInvocation invocation) throws Exception{}方法

该方法的返回值就相当于动作的返回值,也就是在action中写的method方法的返回值了

如果调用了String result = invocation.invoke();得到了动作类(继承了ActionSupport类得动作类)的返回的值。

public String intercept(ActionInvocation invocation) throws Exception {

//判断用户是否登录

HttpSession session = ServletActionContext.getRequest().getSession();

Object obj = session.getAttribute("user");//这个是在用户登录的同时,给用户一个session标记

if(obj==null){

return "login";

}else{

return invocation.invoke();//调用动作方法

}

}

3、拦截器定义好后,一定要在配置文件中进行注册:

<interceptors> 只是定义拦截器,并没有起作用

<interceptor name="permissionInterceptor"class="cn.itcast.interceptor.PermissionInterceptor">
</interceptor>

</interceptors>

4、配置文件中的动作,要通过

<interceptor-ref name="permissionInterceptor"></interceptor-ref>使用该拦截器

注意:一旦动作中使用了自定义的拦截器,那么默认的就不起作用了。一般应该采用如下的做法:

<interceptor-ref name="defaultStack"></interceptor-ref><!--这个是默认的拦截器,一般情况下我们选择默认,但是就算使用了自定义的拦截器,我们还是重新定义一下,并且放置在最前面-->

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

*全局自定义拦截器的话,多个动作类都要使用的话,可以通过package来进行组合。(这个针对多个用户登录)

<package name="myinterceptor" extends="struts-default">

<interceptors>

<interceptor name="PermissionInterceptor"
class="com.itcast.web.interceptor.permissionInterceptor"></interceptor>

<interceptor-stack name="mydefaultStack">

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

<!-- 在这里用定义的拦截器 (*放在最后),以此同时默认的拦截器就没了-->

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

</interceptor-stack>

</interceptors>

</package>

二、案例说明:

继承了interceptor的类permissionInterceptor.class

package com.itcast.web.interceptor;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

/**
* 编写自定义拦截器
* @author Administrator
* 继承拦截器Interceptor接口,
* com.opensymphony.xwork2.interceptor.Interceptor
* 在你所定义的配置文件中,需要给这个自定义的拦截器一个名字
*
*/
public class permissionInterceptor implements Interceptor {

//判断用户登录
public String intercept(ActionInvocation arg0) throws Exception {
HttpSession user=ServletActionContext.getRequest().getSession();
Object ob=user.getAttribute("user");
if(ob==null){
return "login";
}else{
return arg0.invoke();
//调用动作类的返回值就是loginAction中的动作方法默认的是execute返回的success
}
}

public void destroy() {}

public void init() {}

}


action动作类loginAction.class

package com.itcast.web.domain;

import java.io.Serializable;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class loginAction extends ActionSupport implements Serializable {
public String execute(){
return SUCCESS;
}
}


struts.xml配置文件的package内容:

<!-- InterCeptor自定义拦截器 -->
<!-- 登录action -->
<package name="interceptor"  extends="struts-default">
<!-- 给InterCeptor自定义拦截器取名字,只是定义了,但是没有用 -->
<interceptors>
<interceptor name="PermissionInterceptor" class="com.itcast.web.interceptor.permissionInterceptor"><span style="white-space:pre">		</span></interceptor>
</interceptors>

<action name="visitIndex" class="com.itcast.web.domain.loginAction" method="execute">
<!-- 这个是默认的拦截器,在struts-default.xml中可以找到
因为有定义的拦截,所以默认的拦截器被消除了,我们得重新定义一次
-->
<interceptor-ref name="defaultStack"></interceptor-ref>
<!--在这里用定义的拦截器 (*放在最后),以此同时默认的拦截器就没了  -->
<interceptor-ref name="PermissionInterceptor"></interceptor-ref>

<!-- 使用全局动作类自定义拦截器 -->
<result name="success">/Login.jsp</result>
<result name="login">/NotLogin.jsp</result>
</action>
</package>


登录页面的内容NotLogin.jsp:

<h3 align="center">This Is Main Page(Not User)</h3>

<form action="${pageContext.request.contextPath }/UserSessionSet.jsp" method="post">
用户名:<input type="text" name="username"><br/>
密码:<input type="password" name="password"><br/>
<input type="submit" value="提交"/>
</form>


session标记设置页面UserSessionSet.jsp:

<body>
<%
session.setAttribute("user", "");
%>
</body>


session标记去除页面index.jsp

<body>
<%
session.removeAttribute("user");
%>
</body>


登录成功的页面:

<body style="text-align: center;">
<h3 align="center">This Is Main Page(Already User 用户成功登录)</h3><br>
</body>


运行服务器访问过程:

1.先访问项目名/NotLogin.jsp提交到session设置页面UserSessionSet.jsp

2.再访问,项目名/visitIndex.action,这个时候就进入自定义拦截器,搜索到上面设置的session标记,跳转登陆成功页面。

3.访问,session去除页面index.jsp,再访问/visitIndex.action,这个时候就回到登录页面了,没有搜索到session标记,跳转到登录页面。

三。设置全局自定义拦截器,struts.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- constant表示常量设置,设置配置文件自动更新,开发中很重要 -->
<constant name="struts.action.extension" value="action,,do"></constant>
<constant name="struts.devMode" value="true"></constant>
<!-- 设置全局自定义拦截器 -->

<package name="myinterceptor" extends="struts-default">
<interceptors>
<interceptor name="PermissionInterceptor" class="com.itcast.web.interceptor.permissionInterceptor"></interceptor>
<interceptor-stack name="mydefaultStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<!-- 在这里用定义的拦截器 (*放在最后),以此同时默认的拦截器就没了-->
<interceptor-ref name="PermissionInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors>
</package>
<!-- InterCeptor自定义全局拦截器 -->
<!-- 登录action -->
<package name="interceptor"  extends="myinterceptor">
<action name="visitIndex" class="com.itcast.web.domain.loginAction" method="execute">
<!-- 使用全局动作类自定义拦截器 -->
<interceptor-ref name="mydefaultStack"></interceptor-ref>
<result name="success">/Login.jsp</result>
<result name="login">/NotLogin.jsp</result>
</action>
</package>

</struts>


效果和自定义的一样,只不过这个作用于当前xml文件的所有继承myinterceptor包的所有action

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: