1.设计评论页面

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<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:textarea>
<s:submit value="提交"/>
</s:form>
</body>
</html>

2.编写评论成功页面

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>评论成功</title>
</head>
<body>
评论如下:<hr>
评论标题:<s:property value="title"/><br>
评论内容:<s:property value="content"/>
</body>
</html>

3.评论页面对应的业务控制器

PublicAction.java

package action;

import com.opensymphony.xwork2.ActionSupport;

public class PublicAction extends ActionSupport {
    private static final long serialVersionUID =1L;
    private String title;
    private String content;
    public String execute(){return SUCCESS;}
    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;
    }

}

4.编写自定义拦截器

MyInterceptor.java

import java.util.Map;

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

public class MyInterceptor extends AbstractInterceptor {
    private static final long serialVersionUID =1L;
    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[]){
//instanceof 判断value是否为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();
    }
}

5.配置xml

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>benzhu1</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app> 

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>
<package name="default" namespace="/" 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>
<result name="login">/success.jsp</result>
<interceptor-ref name="replace"/> <!– 使用自定义拦截器 –>
<interceptor-ref name="defaultStack"/> <!– Struts2系统默认拦截器 –>
</action>
</package>
</struts>

6.效果图