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开发4--自定义拦截器把不文明用语改变成***相关推荐
- 在struts2中配置自定义拦截器放行多个方法
源码: 自定义的拦截器类: //自定义拦截器类:LoginInterceptor ; package com.java.action.interceptor; import javax.servlet ...
- Struts2第七篇【介绍拦截器、自定义拦截器、执行流程、应用】
什么是拦截器 拦截器Interceptor-..拦截器是Struts的概念,它与过滤器是类似的-可以近似于看作是过滤器 为什么我们要使用拦截器 前面在介绍Struts的时候已经讲解过了,Struts为 ...
- 从struts2拦截器到自定义拦截器
http://www.cnblogs.com/withyou/p/3170440.html 拦截器可谓struts2的核心了,最基本的bean的注入就是通过默认的拦截器实现的,一般在struts2.x ...
- web开发(二十一)之自定义拦截器的使用
转自博客:http://blog.csdn.net/pkgk2013/article/details/51985817 拦截器的作用 拦截器,在AOP(Aspect-Oriented Programm ...
- Struts2自定义类型转换器、自定义拦截器和用户输入数据的验证
一.自定义类型转换器 1.编写一个类,继承com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter 2.覆盖掉其中的public Obj ...
- Struts2 自定义拦截器(方法拦截器)
转自:http://05061107cm.iteye.com/blog/365504 struts2系统自带了很多拦截器,有时需要我们自己定义,一般有两种方式: 一.实现Interceptor接口 J ...
- Struts2内置拦截器和自定义拦截器
内置拦截器 Struts2中内置类许多的拦截器,它们提供了许多Struts2的核心功能和可选的高级特性.这些内置的拦截器在struts-default.xml中配置.只有配置了拦截器,拦截器才可以正常 ...
- Struts2 自定义拦截器(easy example)
要自定义拦截器需要实现com.opensymphony.xwork2.interceptor.Interceptor接口: 新建一个MyIntercept package com.action;imp ...
- struts2自定义拦截器一——模拟登陆权限验证
1.http://localhost:8083/struts2/user.jsp 表示用户已登陆,存放session对象 2.http://localhost:8083/struts2/quit.js ...
最新文章
- Android开源框架ImageLoader的完美例子
- 用类模拟C风格的赋值+返回值
- 【最新合集】编译原理习题(含答案)_15运行存储分配_MOOC慕课 哈工大陈鄞
- 前端学习(3008):vue+element今日头条管理--登录中的loding
- MAT插件分析内存泄露之二
- Effective C++ 条款 50:了解new和delete的合理替换时机
- lfw分类 python_无法在sklearn中使用LFW数据集
- 多张图片怎么合成一个pdf?
- win 10 桌面路径还原到C盘拒绝访问
- 2022年8月22日 暑假第六周总结
- linux 查看隐藏文件大小,Linux运维知识之linux下显示隐藏目录或隐藏文件占用空间大小...
- containers和overlay2占用磁盘过大
- DSP TI6657 执行pdkProjectCreate.bat问题解决办法
- iOS应⽤签名原理浅析
- 【艺术硕士论文】现代婚嫁服饰中传统缠花艺术的运用分析(节选)
- AMESim锂离子电池包电化学机理模型
- 关于DeviceLock和QQ2005正式版
- 给微软免费的文本转语音网站添加下载按钮
- 卷毛崽|Linux自学|存储结构与磁盘划分
- Visual SVN Server启动失败0x8007042a错误