例题:将输入内容中的讨厌变成喜欢
1. 配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts Blank</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>
2. action类
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 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(){
if (title.contains("讨厌")) {
title =title.replaceAll("讨厌", "喜欢");
}
if (content.contains("讨厌")) {
content =content.replaceAll("讨厌", "喜欢");
}
return SUCCESS;
}
}
3.编写拦截器
package interceptor;
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;
@Override
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
}
}
4. 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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<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>
5. 页面
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<title>评论</title>
</head>
<body>
请发表你的评论!
<hr>
<form action="public" method="post">
评论标题: <input name="title" maxLength="36" /><br>
评论内容: <textarea name="content" cols="36" rows="6"></textarea>
<br>
<input type="submit" value="提交" />
</form>
</body>
</html>
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head> <title>评论成功</title> </head>
<body>
评论如下:<hr>
评论标题2:${title} <br>
评论内容2:${content}
</body>
</html>