拦截器,在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作。拦截是AOP的一种实现策略。
拦截器是动态拦截Action调用的对象。它提供了一种机制可以使开发者可以定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行。同时也是提供了一种可以提取action中可重用的部分的方式。
拦截器栈:为了能在多个动作中方便地引用同一个或者几个拦截器,可以使用拦截器栈将这些拦截器作为一个整体来引用。当拦截器栈被附加到一个Action时,想执行Action,必须限制性拦截器栈中的每一个拦截器。
拦截器栈:
<interceptors> <interceptor-stack name="拦截器栈名"> <interceptor-ref name="拦截器名"> </interceptor-stack> </interceptors>
拦截器的使用:
<action name="user" class="com.action.UserAction"> <result>/user.jsp</result> <interceptor-ref name="拦截器名"> </action>
应用的小例子:
Struts.xml
<struts>
<package name="strut2" extends="struts-default" namespace="/">
<interceptors>
<interceptor name="loginValidate" class="com.sun.common.LoginInterceptor"/>
<interceptor name="ContentValidate" class="com.sun.common.ContentInterceptor"/>
</interceptors>
<default-interceptor-ref name="myInterceptorStack" />
<action name="login" class="com.sun.controller.LoginController">
<result name="success">/return.jsp</result>
<result name="login">/error.jsp</result>
<!-- 使用自定义拦截器,校验用户是否登录 -->
<interceptor-ref name="loginValidate" />
</action>
<action name="content" class="com.sun.controller.ContentController">
<result name="success">/return.jsp</result>
<result name="login">/error.jsp</result>
<!-- 使用自定义拦截器,校验用户是否登录 -->
<interceptor-ref name="ContentValidate" />
</action>
</package>
</struts>
web.xml
<filter>
<!--配置Struts2 核心 Filter的名字 -->
<filter-name>struts2</filter-name>
<!--配置struts2 核心 FIlter的实现类 -->
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<!-- 设定初始化参数 -->
<init-param>
<!-- 指定要加载的配置文件列表,多个用逗号分开 ,注:如果-->
<!-- 注:如果配置的config,则必须把默认的文件谢进入,否则报错 -->
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,struts.xml,struts-config.xml</param-value>
</init-param>
</filter>
<!-- 配置Filter拦截的 URL -->
<filter-mapping>
<!--过滤器拦截名称 -->
<filter-name>struts2</filter-name>
<!-- 配置 Struts2 核心 FilterDispatcher拦截所有.action用户请求 -->
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
LoginInterceptor.java
public class LoginInterceptor extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation ai) throws Exception {
Map session = ai.getInvocationContext().getSession();//获得session对象
//获取session中的username对象,并赋值给uname变量
String uname = (String)session.get("username");
//判断是否为空,如不为null,则进行后续操作
if (uname != null && !"".equals(uname)) {
return ai.invoke();
}else {
session.put("errorMsg", "您还未登录,请登录");
return Action.LOGIN;
}
}
}
ContentInterceptor.java
public class ContentInterceptor extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation ai) throws Exception {
//得到表单中内容
Map<String,Object> map = ai.getInvocationContext().getParameters();
String[] content = (String[])map.get("content");
if (content[0] != null && !"".equals(content[0])){//如果有评论信息
int startIndex = content[0].indexOf("人品差");//检测人字出现的位置
//如果存在“人”字,且人字后有2个字
if (startIndex != -1) {
content[0] = content[0].replaceAll("人品差","***");
ai.getInvocationContext().getSession().put("content", content[0]);
return ai.invoke();//程序继续运行
}else {
ai.getInvocationContext().getSession().put("content", content[0]);
return ai.invoke();//程序继续运行
}
}
return Action.LOGIN;
}
}