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 明星 ...
随机推荐
- linux下获取本机的获取内网和外网地址
1.获取内网地址(私有地址) ifconfig -a 2.获取外网地址(公网地址) curl members.3322.org/dyndns/getip
- <offer4> 04_FindInPartiallySortedMatrix
#include<cstdio> bool Find(int* matrix, int rows, int columns, int number) { bool result = fal ...
- HDU 1686 Oulipo(KMP)题解
题意:主串中能找到几个模式串 思路:超详细解释KMP KMP:针对这个代码,解释一下Fail数组的含义:T为主串,P为模式串,Fail代表失配值,即当P[j] != T[i]时,j要指向的位置为Fai ...
- excel依赖的dll
依赖的先后顺序 stdole.dll office.dll Microsoft.Vbe.Interop.dll Microsoft.Office.Interop.Excel.dll Interop是i ...
- 51nod 1242 斐波那契数列的第N项
之前一直没敢做矩阵一类的题目 其实还好吧 推荐看一下 : http://www.cnblogs.com/SYCstudio/p/7211050.html 但是后面的斐波那契 推导不是很懂 前面讲的挺 ...
- BZOJ1304: [CQOI2009]叶子的染色 树形dp
Description 给一棵m个结点的无根树,你可以选择一个度数大于1的结点作为根,然后给一些结点(根.内部结点和叶子均可)着以黑色或白色.你的着色方案应该保证根结点到每个叶子的简单路径上都至少包含 ...
- 获取String类型汉字乱码,如何进行编码
本文为博主原创,未经允许不得转载: 在解析properties文件中的汉字时,在java代码中解析得到的是一个乱码字符,形如图下: 导致乱码原因:由于在jdk中,默认为gbk编码方式进行编码盒接收的, ...
- Ubuntu 14.04 安装 qemu
参考: Ubuntu 12.04之找不到Qemu命令 Ubuntu 14.04 安装 qemu 安装: sudo apt-get install qemu 使用ln命令建立软连接: sudo ln - ...
- MVC ---- EF的延迟加载
//EF中的where 有延迟加载功能(Iqueryable中的where) Sys_Log pEdit = nb.Sys_Log.Where(p=>p.F_Account== "su ...
- python计算结果显示小数
先将整型转换成float型,在计算,结果就有小数了 >>> a = >>> b = >>> c = a/b >>> a,b,c ...