Struts2自定义拦截器——完整实例代码
比如一个网上论坛过滤系统,将网友发表的不文明、不和谐的语言,通过拦截器对这些文字进行自动替代。
该项目包含:
1、自定义拦截器(MyInterceptor.java)
2、发表评论的页面(news.jsp)
3、对应的业务控制器(PublicAction.java)
4、业务控制器控制其跳转到success.jsp
代码如下:
1、自定义拦截器(MyInterceptor.java)
package interceptor; import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class MyInterceptor extends AbstractInterceptor{ public String intercept(ActionInvocation ai) throws Exception {
//获取Action的实例
Object object = ai.getAction();
if(object!=null){
if(object instanceof PublicAction){
PublicAction ac=(PublicAction)object; //获取用户提交的评论内容
String content=ac.getContent();
//判断用户提交的评论内容是否有要过滤的内容
if(content.contains("讨厌")){
//以“喜欢”代替要过滤的“讨厌”
content = content.replaceAll("讨厌","喜欢");
//把替代后的评论内容设置为Action的评论内容
ac.setContent(content);
}
//对象不空,继续执行
return ai.invoke(); }else{
//返回Action中的LOGIN逻辑视图字符串
return Action.LOGIN;
}
} return Action.LOGIN;
} }
2、发表评论的页面(news.jsp)
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ 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>
请发表你的评论:
<hr>
<s:form action="public" method="post">
<s:textfield name="title" label="评论标题" maxLength="" />
<s:textarea name="content" rows="" cols="" label="评论内容" />
<s:submit value="提交" />
</s:form> </body>
</html>
3、对应的业务控制器(PublicAction.java)
package interceptor;
import com.opensymphony.xwork2.ActionSupport;
public class PublicAction extends ActionSupport{
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() throws Exception{
return "success";
}
}
4、业务控制器控制其跳转到success.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ 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>
评论如下:
<hr>
评论标题:<s:property value="title" />
<br>
评论内容:<s:property value="content" /> </body>
</html>
5、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> <package name="intercept" extends="struts-default">
<interceptors>
<interceptor name="replace" class="interceptor.MyInterceptor" ></interceptor>
</interceptors> <action name="public" class="interceptor.PublicAction">
<result name="success">/success.jsp</result>
<result name="login">/success.jsp</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="replace"></interceptor-ref>
</action> </package> </struts>
6、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"> <filter>
<!-- 配置Struts2核心Filter的名字 -->
<filter-name>struts2</filter-name>
<!-- 配置Struts2核心Filter的实现类 -->
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- 配置Filter拦截的URL -->
<filter-mapping>
<!-- 配置Struts2的核心FilterDispatcher拦截所有用户请求 -->
<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>
Struts2自定义拦截器——完整实例代码的更多相关文章
- Struts2自定义拦截器Interceptor以及拦截器登录实例
1.在Struts2自定义拦截器有三种方式: -->实现Interceptor接口 public class QLInterceptorAction implements Interceptor ...
- struts2自定义拦截器 设置session并跳转
实例功能:当用户登陆后,session超时后则返回到登陆页面重新登陆. 为了更好的实现此功能我们先将session失效时间设置的小点,这里我们设置成1分钟 修改web.xml view plainco ...
- 【Java EE 学习 35 下】【struts2】【struts2文件上传】【struts2自定义拦截器】【struts2手动验证】
一.struts2文件上传 1.上传文件的时候要求必须使得表单的enctype属性设置为multipart/form-data,把它的method属性设置为post 2.上传单个文件的时候需要在Act ...
- 12.Struts2自定义拦截器
12.自定义拦截器 拦截器是Struts2的一个重要特性.因为Struts2的大多数核心功能都是通过拦截器实现的. 拦截器之所以称之为“拦截器”,是因为它可以拦截Action方法的执行, ...
- Struts2 自定义拦截器
自定义拦截器(权限管理),包含了对ajax和表单请求的拦截 package com.interceptor; import java.io.IOException; import java.io.Pr ...
- struts2自定义拦截器与cookie整合实现用户免重复登入
目的:测试开发时,为了减少用户登入这个繁琐的登入验证,就用struts2做了个简单的struts2拦截器,涉及到了与cookie整合,具体的看代码 结构(两部份)=struts2.xml+自定义拦截器 ...
- Struts2自定义拦截器处理全局异常
今天在整理之前的项目的时候想着有的action层没有做异常处理,于是想着自定义拦截器处理一下未拦截的异常. 代码: package cn.xm.exam.action.safeHat; import ...
- Struts2自定义拦截器
1. 需求 自定义拦截器实现,用户登录的访问控制. 2. 定义拦截器类 public class LoginInterceptor extends AbstractInterceptor { @Ove ...
- 5、Struts2自定义拦截器
一.拦截器相关知识 1.Struts2框架剖析 Holly版本生活案例: 影视公司(拍电影) ActionMapper 传媒公司(包装明星) ActionMapping 明星 ...
随机推荐
- Python3基础 str swapcase 英文字母大小写反转
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- JS控制页面内容
JS操作页面内容 innerText:普通标签内容(自身文本与所有子标签文本)innerHTML:包含标签在内的内容(自身文本及子标签的所有)value:表单标签的内容outerHTML:包含自身标签 ...
- 完整的Android开发环境Eclipse+ADT+SDK(22.0.1)
现在开始学习Android嵌入式编程,首要的问题就是在Windows中搭建开发环境,就这个都要摸索很长的时间,总是在版本之间折腾折腾去,而且Google的Android正式差劲得很,经常是连不上,要不 ...
- CF873B Balanced Substring
1到n内0,1个数相同的个数的最长字串 \(i>=j\) \[1的个数=0的个数\] \[sum[i]-sum[j-1]=i-(j-1) - (sum[i]-sum[j-1])\] 这里把\(( ...
- 联合权值dp
联合权值 洛谷中可找到 题目传送门https://www.luogu.org/problemnew/show/P1351 这题我就得了70分(TLE) GG了 就是遍历它孩子的孩子(爷爷和孙子),然 ...
- 小Z的袜子(莫队分块)题解
小Z的袜子(hose) 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……具体来说,小Z把这N只袜 ...
- Python time strptime()与time strftime()
time strftime()接收时间元组,返回表示时间的字符串. time strptime()把时间字符串,解析成一个时间元组. import time t = time.strftime('%Y ...
- Windows上玩转TensorFlow(一)
Windows上TensorFlow的安装和环境搭建: 1.安装Python 3.5.2 2.通过Pip3安装TensorFlow CPU版 https://www.tensorflow.org/in ...
- ADO.NET 批量插入
在.Net1.1中无论是对于批量插入整个DataTable中的所有数据到数据库中,还是进行不同数据源之间的迁移,都不是很方便.而 在.Net2.0中,SQLClient命名空间下增加了几个新类帮助我们 ...
- signal_windows
1.Qt532(vs2010 opengl) // ZC: windows signal: // http://blog.csdn.net/mergerly/article/details/79521 ...