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基础 __repr__ 实例对象的名字,可以显示信息
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- springmvc-自定义消息转换器
最近的项目没有用到这个,先把自己自学跑通的例子先帖出来,供自己以后参考吧! 如有不对地方望指出! 一.自定义类实现AbstractHttpMessageConverter package com.dz ...
- UVa 1151 买还是建
https://vjudge.net/problem/UVA-1151 题意: 平面上有n个点,你的任务是让所有n个点连通.为此,你可以新建一些边,费用等于两个端点的距离平方和.另外还有q个套餐可以购 ...
- IntelliJ IDEA问题总结
在使用Idea的过程中,会遇到各种各样的问题,下面我将在这里持续总结: 1.Unable to import maven project: See logs for details 在遇到这个问题时, ...
- poj 2385 Apple Catching 基础dp
Apple Catching Description It is a little known fact that cows love apples. Farmer John has two ap ...
- ubuntu14.04上 nginx启动停止
sudo service nginx stop 停止 sudo nginx 启动
- map/multimap_01
标准的关联式容器 键值对序列 基于key的快速检索能力 key按序排列,按序插入 红黑树变体的平衡二叉树 对key来说支持 mapT[key] 和 mapT.at(key) multimap 不支持 ...
- [ios]Xcode常用快捷键
参考:http://www.linuxidc.com/Linux/2012-08/67905.htm Xcode常用快捷键 隐藏xcode command+h退出xcode command+q关闭窗口 ...
- String字符串存入数据库中超出最大长度(oracle varchar2 4000)?应合理分条存储(java实现-工具/方法)
问题描述 需要向数据库中保存数据,但某个字段内容长度过长(有中文.符号.英文),应该根据字符串内容与数据库存储上限合理设置储存方式. 解决思路 分条存储,即多条数据前n个字段一致,最后内容字段不同,下 ...
- dd 命令常用功能收集(ing...)
xu言: 发现自己老是忘记一些不怎么常用,但是一定会用到的命令...so,做个备忘吧 Tips: sudo sh -c "head -c 15M /dev/urandom > test ...