struts2 文字过滤拦截器
来源:互联网 发布:C语言下列叙述错误的是 编辑:程序博客网 时间:2024/06/02 01:58
假设我拦截掉文本中的“脑残”二字
MyInterceptor
类 该类为文字拦截器类
该类继承AbstractInterceptor
package com.xxx.interceptor;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;import com.xxx.action.PublicAction;public class MyInterceptor extends AbstractInterceptor { @Override public String intercept(ActionInvocation ai) throws Exception { //获取action实例 Object obj = ai.getAction(); if(obj!=null){ if(obj instanceof PublicAction){ PublicAction action = (PublicAction)obj; String content = action.getContent(); System.out.println("intercept方法,修改前[content]="+content); if(content.contains("脑残")){ content = content.replaceAll("脑残", "*"); System.out.println("intercept方法,修改后[content]="+content); action.setContent(content); } return ai.invoke(); }else{ return Action.LOGIN; } }else{ return Action.LOGIN; } }}
文本输入html页面
提交后也跳转到这页面看看数据
<%@taglib prefix="s" uri="/struts-tags"%>....<body> <form action="public" method="post"> <p> 文本内容: <input type="text" name="content" /> </p> <input type="submit" value="提交"> </form> <p> 测试过滤后的文本: <s:property value="content" /> </p></body>
com.xxx.action
包下的PublicAction类
package com.xxx.action;import com.opensymphony.xwork2.ActionSupport;public class PublicAction extends ActionSupport { private String content;//文字内容 /**忽略content属性的setXxx()和getXxx()方法 */ @Override public String execute() throws Exception { System.out.println("execute()方法[content=]"+content); return SUCCESS; }}
struts.xml
配置
<struts> <package name="xuexi" namespace="/" extends="struts-default"> <!-- 定义拦截器 --> <interceptors> <interceptor name="replace" class="com.xxx.interceptor.MyInterceptor" /> </interceptors> <action name="public" class="com.xxx.action.PublicAction"> <result name="success">/NewFile1.jsp</result> <result name="login">/NewFile1.jsp</result> <!-- 使用拦截器 --> <interceptor-ref name="defaultStack" /> <interceptor-ref name="replace" /> </action> </package></struts>
以上都配置好后 完成咯~
看得出 HTML数据→拦截器→(过滤)→Action
阅读全文
0 0
- struts2-拦截器 过滤文字
- struts2 文字过滤拦截器
- struts2拦截器-文字过滤拦截器实例
- 文字过滤拦截器
- jsp struts2 学习(五) ----- 文字过滤拦截器
- ssh框架学习-struts2 使用自定义拦截器进行文字过滤拦截
- struts拦截器文字过滤拦截器
- struts2拦截器实现session会话过滤
- Struts2拦截器的方法过滤
- Struts2拦截器的方法过滤
- 【Struts2】:拦截器实现方法过滤
- Struts2拦截器之拦截器的方法过滤
- Struts2拦截器之拦截器的方法过滤
- Struts2拦截器之拦截器的方法过滤
- struts2拦截器值拦截器的方法过滤
- struts2--实现文字过滤功能
- struts2文件过滤拦截器fileUpload以及各种文件类型
- struts2文件过滤拦截器fileUpload以及各种文件类型
- 开篇
- 文章标题
- JS修改属性的默认特性(属性描述符)
- 对设计模式的理解
- C# mvc 中把带 html标签的字符 当作 html标签 显示到页面
- struts2 文字过滤拦截器
- 深入浅出妙用 Javascript 中 apply、call、bind
- 关于最后
- LeetCode113. Path Sum II
- 通过OllyDbg调试windows窗口程序
- 【Spring笔记】2. XML装配Bean
- 11.服务链路追踪(sleuth+zikpin)
- 洛谷P3384 【模板】树链剖分
- 深入理解计算机系统--笔记