struts2开发4--自定义拦截器把不文明用语改变成***

struts2拦截器是在访问某个Action或者Action的某个方法、字段之前或者之后实施拦截,并且struts2拦截器是可插拔的,拦截器是AOP的一种实现。这里重点介绍下自定义文字过滤拦截器,把我们平时评论中的不文明的用语改变成***显示在评论对话框中。具体操作如下几步完成:(参考程序是把评论中“人品差”改变成“***”,大家可以利用这个方法,根据自己项目需求,设定其他不文明用语)

第一步,在项目中创建ContentAction类,具体代码如下:

package cn.test.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ContentAction extends ActionSupport{
 private String name;//评论人
 private String content;//评论内容
 public String contentUI()throws Exception{
  return "content";
 }
   
 public String execute() throws Exception{
  ActionContext ac=ActionContext.getContext();
  ac.getApplication().put("name", name);
  ac.getApplication().put("content", content);
     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;
 }
}
第二步,创建content_send.jsp,代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<s:form action="content_execute">
<s:textfield name="name" label="评论人" size="81"></s:textfield>
<s:textarea name="content" label="评论正文" clos="80" rows="10"></s:textarea>
<s:checkbox name="arr" label="我已经阅读并同样条款"></s:checkbox>
<s:submit value="发送" ></s:submit>
</s:form>
</html>

第三步,创建content_success.jsp,代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="s"  uri="/struts-tags"%>
<table >
  <tr style="font-size: 12px">
   <td>评论人 :<s:property value="#application.name"/></td>
  </tr>
  <tr>
    <td style="font-size: 12px"> 评论正文: <s:property value="#application.content"/></td>
  </tr>
</table>
</html>

第四步:创建自定义过滤器ContentInterceptor,代码如下:

package cn.test.Interceptor;

import cn.test.action.ContentAction;

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

public class ContentInterceptor extends AbstractInterceptor{

 @Override
 public String intercept(ActionInvocation arg0) throws Exception {
  
  Object obj=arg0.getAction();//获取Action的实例
  if(obj!=null)
  {
   if(obj instanceof ContentAction)
   {
    ContentAction ca=(ContentAction)obj;//实例化ContentAction类
    //System.out.println(ca);
    String content=ca.getContent();//获得用户提交的评论信息
    //System.out.println(content);
    if(content!=null)
    {
    int startIndex=content.indexOf('人');//检测字符人出现的位置
    String str=content.substring(startIndex,startIndex+3);
    if(str.equals("人品差"))
    {
     content=content.replaceAll("人品差", "***");
     ca.setContent(content);
    }
    return arg0.invoke();//放行
    }
    return arg0.invoke();
   }
  return arg0.invoke();
  }
  return arg0.invoke();
 }
}

第五步,配置struts.xml文件

 <!-- 定义拦截器声明 -->
   <interceptors>
    <interceptor name="ContentInterceptor" class="cn.test.Interceptor.ContentInterceptor"></interceptor>
    </interceptors>

<action name="content_*" class="cn.test.action.ContentAction" method="{1}">

                       <!-- 在Action中使用声明的拦截器 -->
        <interceptor-ref name="defaultStack"></interceptor-ref>
        <interceptor-ref name="ContentInterceptor"></interceptor-ref>
        <result name="content">/WEB-INF/jsp/content_send.jsp</result>
        <result name="success">/WEB-INF/jsp/content_success.jsp</result>

大家注意一下,默认拦截器一定使用在自定义拦截器前面,否则,不能实现拦截

第六步:部署项目,启动tomcat,在浏览器中输入:http://localhost:8080/MyWeb/content_contentUI

测试结果:

 

点击发送

 

 

 

你可能感兴趣的:(struts2,拦截器Interceptor)