说明
上一篇说拦截器的主要作用,看到一个例子特别有趣,写出来。这里是一个自定义的拦截器。在对商品评价时会出现不文明的字词,这里用“ * ”来替换掉文本中不文明的内容,例如“cao”。这里不想再去配置Struts的jar包了,所以用之前一个很小的例子继续写下去,遇到jar包问题可以看一下https://blog.youkuaiyun.com/hansmu/article/details/102212523
目录
看一下目录先,那些之前写过的不需要用到的文件我会马赛克掉
步骤
在com.shanmu.action包中创建ContentAction类,代码如下:
package com.shanmu.action;
public class ContentAction {
private String name;//评论人
private String content;//评论内容
public String inputContent(){
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
在com.shanmu.interceptor包下创建ContentInterceptor类,该类为自定义拦截器类,这里继承AbstractInterceptor类,不太清楚拦截器知识的话,可以看一下https://blog.youkuaiyun.com/hansmu/article/details/102303766
代码如下:
package com.shanmu.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.shanmu.action.ContentAction;
public class ContentInterceptor extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
Object obj=invocation.getAction();//获取Action实例
if(obj!=null){//如果不为空
if(obj instanceof ContentAction){//如果obj是ContentAction类型的
ContentAction action=(ContentAction)obj;//实例化ContentAction类
String content=action.getContent();//获得用户的评论信息
int startIndex=content.indexOf('c');//检测到”c“的位置
String str=content.substring(startIndex,startIndex+3);//取从”c“开始的三个字符
if(str.equals("cao")){//如果这三个字是”cao“
content=content.replaceAll("cao","*");//用*替换掉这三个字
action.setContent(content);//将替换后的内容赋给content属性
}
return invocation.invoke();//调用invoke方法,程序通过此拦截器
}else{
return "login";
}
}
return "login";
}
}
接下来在struts.xml文件中配置ContentInterceptor拦截器,并在ContentAction类的配置中使用该拦截器,配置内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 指定字符编码集 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!-- 配置包 -->
<package name="basicstruts" extends="struts-default">
<!-- 自定义拦截器 -->
<interceptors>
<interceptor name="contentItp" class="com.shanmu.interceptor.ContentInterceptor"/>
</interceptors>
<!-- 配置Action -->
<action name="content" class="com.shanmu.action.ContentAction" method="inputContent">
<result>/content_success.jsp</result>
<result name="login">/content_send.jsp</result>
<!-- 使用系统默认拦截器 -->
<interceptor-ref name="defaultStack"/>
<!-- 使用自定义拦截器 -->
<interceptor-ref name="contentItp"/>
</action>
</package>
</struts>
下面是界面了,在WebContent目录下新建content_send.jsp页面,用来让用户发表评论。
代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false" %>
<%@ 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>
<s:form action="content" method="post">
<s:textfield name="name" label="评论人" size="81"/>
<s:textarea name="content" label="评论正文" cols="80" rows="10"/>
<s:checkbox name="message" label="我已阅读并同意网站规则"/>
<br/>
<s:submit value="发表评论"/>
</s:form>
</body>
</html>
在WebContent目录下新建content_success.jsp页面,评论成功后显示评论,代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false" %>
<%@ 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>Insert title here</title>
</head>
<body>
<table cellpadding="0" cellspacing="0" border="0" align="left">
<tr style="line-height:30px;">
<td style="font-size:14px;font-weight:bold;" align="left">
对《天才在左,疯子在右》的评论
</td>
</tr>
<tr>
<td style="font-size:12px">
评论人:${name }
</td>
</tr>
<tr>
<td style="font-size:12px">
评论内容:${content }
</td>
</tr>
</table>
</body>
</html>
运行结果
因为添加了拦截器,所以把脏话给替换掉了。