什么是过滤器
- Struts2 拦截器在访问某个 Action 方法之前或之后实施拦截, Struts2 拦截器是可插拔的, 拦截器是 AOP 的一种实现
- 拦截器栈(Interceptor Stack): 将拦截器按一定的顺序联结成一条链. 在访问被拦截的方法时, Struts2拦截器链中的拦截器就会按其之前定义的顺序被依次调用
自定义拦截器
步骤
- 自定义一个实现Interceptor接口(继承AbstractInterceptor或继承MethodFilterIntercepter)的类。
- 在struts.xml中注册上一步中定义的拦截器
- 在需要使用的Action中引用上述定义的拦截器
实例讲解----文字过滤器的设计与应用
实例:开发一个网上论坛过滤系统,如果网友发表的有不文明的语言,将通过拦截器对不文明的文字进行自动替代。只是给出了一种简单的过滤,过滤是否有“讨厌”文字,若有“讨厌”,则用“喜欢”代替要过滤的内容“讨厌”,形成新的文本内容并显示在论坛上。
自定义拦截器
package interceptor;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class MyInterceptor extends AbstractInterceptor {
public String intercept(ActionInvocation ai) throws Exception {
//获取页面提交的所有属性及其属性值
Map<String, Object> parameters = ai.getInvocationContext().getParameters();
//对每对属性、属性值分别进行过滤,将过滤后的内容再保存到该属性中
for (String key : parameters.keySet()) {
Object value = parameters.get(key);
if ( value != null && value instanceof String[]) {
String[] valueArray = (String[]) value;
for (int i = 0; i < valueArray.length; i++) {
if( valueArray[i] != null ){
//判断用户提交的评论内容是否有要过滤的内容
if(valueArray[i].contains("讨厌")) {
//以“喜欢”代替要过滤的内容“讨厌”
valueArray[i] =valueArray[i].replaceAll("讨厌", "喜欢");
//把替代后的评论内容设置为Action的评论内容
parameters.put(key, valueArray);
}
}
}
}
}
return ai.invoke();//进行执行下一个拦截器或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>
<constant name="struts.custom.i18n.resources" value="globalMessages"/>
<constant name="struts.i18n.encoding" value="UTF-8" />
<package name="I18N" extends="struts-default">
<interceptors>
<!--文字过滤拦截器配置,replace 是拦截器的名字-->
<interceptor name="replace" class="interceptor.MyInterceptor" />
</interceptors>
<action name="public" class="action.PublicAction"><!--文字过滤Action配置-->
<result name="success">/success.jsp</result>
<interceptor-ref name="replace"/> <!--使用自定义拦截器-->
<interceptor-ref name="defaultStack" /> <!--Struts2系统默认拦截器-->
</action>
</package>
</struts>
编写Action引用自定义的拦截器
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package action;
import com.opensymphony.xwork2.ActionSupport;
/**
*
* @author lenovo
*/
public class PublicAction extends ActionSupport {
private String title;
private String content;
//属性的getter、setter方法
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String execute() {
return SUCCESS;
}
}
界面设计
- 评论界面
index.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head> <title>评论</title> </head>
<body>
请发表你的评论!<hr>
<s:form action="public" method="post">
<s:textfield name="title" label="评论标题" maxLength="36"/>
<s:textarea name="content" cols="36" rows="6" label="评论内容"/>
<s:submit value="提交"/>
</s:form>
</body>
</html>
- 评论完,过滤后返回的界面
success.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head> <title>评论成功</title> </head>
<body>
评论如下:<hr>
评论标题:<s:property value="title"/> <br>
评论内容:<s:property value="content"/>
</body>
</html>
结果