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

Struts2拦截方法拦截器的使用(十三)

2017-12-28 13:30 513 查看
(一)拦截方法拦截器

1.说明

1.配置自定义拦截器拦截某个Action,该拦截器会拦截该Action的所有方法。

2.某些情况下我们并不希望拦截一个Action类的所有方法,那么就需要使用

拦截方法拦截器。

3.Struts2提供MethodFilterInterceptor类,该类是AbstractInterceptor的

子类,可以实现对Action方法的拦截。 重写doIntercept方法。

2.注意点

MethodFilterInterceptor中参数的配置

1.如果不配置这两个参数,Action中的所有方法都会被拦截。

2.如果一个方法同时在excludeMethods和includeMethods中出现,则会被拦截。

3.配置拦截方法拦截器

a.拦截方法拦截器提供两个参数分别是:

1.excludeMethods:不需要拦截的方法,,不需要拦截多个方法逗号隔开。

2.includeMethods:需要拦截的方法,拦截多个方法逗号隔开。

(二)实例演示

1.创建Action类

package com.wang;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
/**
*
* @author Kaina
* 2017-12-28
*
*/
public class CRUDOperation extends ActionSupport {

private static final long serialVersionUID = 1L;
public String add(){
System.out.println("添加一些实现添加功能的业务逻辑代码!");
return Action.SUCCESS;
}
public String query(){
System.out.println("添加一些实现查询功能的业务逻辑代码!");
return Action.SUCCESS;
}
public String update(){
System.out.println("添加一些实现修改功能的业务逻辑代码!");
return Action.SUCCESS;
}
public String delete(){
System.out.println("添加一些实现删除功能的业务逻辑代码!");
return Action.SUCCESS;
}

}


2.创建拦截器

package com.intercepter;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class MyFilterIntercepter  extends MethodFilterInterceptor{

private static final long serialVersionUID = 1L;

@Override
//1.在此方法类,编写拦截业务逻辑
protected String doIntercept(ActionInvocation invocation) throws Exception {
System.out.println("方法拦截器被执行,无法请求此url");
//return invocation.invoke();
//2.拦截一些方法,并返回到其他页面
return Action.LOGIN;
}

}


3.在struts.xml中配置Action和拦截器

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC

d605
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="wang" namespace="/" extends="struts-default">
<interceptors>
<!--1.定义一个拦截器-->
<interceptor name="myfilterintercptor" class="com.intercepter.MyFilterIntercepter">
<!--01.不需要被拦截的方法-->
<param name="excludeMethods">add,query</param>
<!--02.需要被拦截的方法-->
<param name="includeMethods">update,delete</param>
</interceptor>
<!--2.定义一个拦截器栈-->
<interceptor-stack name="mydefault">
<interceptor-ref name="myfilterintercptor"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<!--3.配置默认拦截器对整个包下的所有action都有用-->
<default-interceptor-ref name="mydefault"/>
<global-results>
<result name="login">/error.jsp</result>
</global-results>
<!--4.配置一个Action中的四个方法-->
<action name="add" class="com.wang.CRUDOperation" method="add">
<result name="success">/success.jsp</result>
</action>
<action name="query" class="com.wang.CRUDOperation" method="query">
<result name="success">/success.jsp</result>
</action>
<action name="update" class="com.wang.CRUDOperation" method="update">
<result name="success">/error.jsp</result>
</action>
<action name="delete" class="com.wang.CRUDOperation" method="delete">
<result name="success">/error.jsp</result>
</action>
</package>
</struts>


4.编写一个JPS页面

<body>
<!--1.分别请求一个Action类中的四个方法-->
<!--2.通过配置拦截器,拦截一些请求和不拦截一些请求-->
<a href="add">添加用户</a>
<a href="query">查询用户</a>
<a href="delete">删除用户</a>
<a href="update">修改用户</a>
</body>


5.结果演示

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