struts2的拦截器,替换掉敏感词汇
2016-01-26 23:18
302 查看
以下是输入界面
news.jsp
<%@ page language="java" 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>
<h3>请发表你的评论</h3>
<hr/>
<s:form action="public" method="post">
<s:textfield name="title" label="评论标题" maxLength="36"></s:textfield>
<s:textarea name="content" cols="36" rows="6" label="评论内容"/>
<s:submit value="submit"></s:submit>
</s:form>
</body>
</html>
success.jsp
<%@ page language="java" 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>
success成功!
<hr/>
评论标题:<s:property value="title"/><br/>
评论内容:<s:property value="content"/>
</body>
</html>
以下是,java文件
PublicAction.java
package interceptor;
import com.opensymphony.xwork2.ActionSupport;
public class PublicAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private String title;
private String content;
public void setTitle(String title){
this.title=title;
}
public String getTitle(){
return title;
}
public void setContent(String content){
this.content=content;
}
public String getContent(){
return content;
}
public String execute(){
return SUCCESS;
}
}
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 {
@Override
public String intercept(ActionInvocation ai) throws Exception {
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("讨厌", "喜欢");
ac.setContent(content);
}
if(content.contains("毛泽东")){
content=content.replaceAll("毛泽东", "毛主席");
ac.setContent(content);
}
//获取标题,并对特殊字眼进行转换后显示
String title=ac.getTitle();
if(title.contains("苹果")){
title=content.replaceAll("苹果", "水果");
ac.setTitle(title);
}
return ai.invoke();
}else{
return Action.LOGIN;
}
}
return Action.LOGIN;
}
}
以下是struts.xml的配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="interceptor" namespace="/" extends="struts-default">
<!-- 文件过滤拦截器配置,replace是拦截器的名字 -->
<interceptors>
<interceptor name="replace" class="interceptor.MyInterceptor"></interceptor>
</interceptors>
<action name="public" class="interceptor.PublicAction">
<result name="success">/success.jsp</result>
<result name="login">/news.jsp</result>
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="replace"/>
</action>
</package>
</struts>
news.jsp
<%@ page language="java" 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>
<h3>请发表你的评论</h3>
<hr/>
<s:form action="public" method="post">
<s:textfield name="title" label="评论标题" maxLength="36"></s:textfield>
<s:textarea name="content" cols="36" rows="6" label="评论内容"/>
<s:submit value="submit"></s:submit>
</s:form>
</body>
</html>
success.jsp
<%@ page language="java" 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>
success成功!
<hr/>
评论标题:<s:property value="title"/><br/>
评论内容:<s:property value="content"/>
</body>
</html>
以下是,java文件
PublicAction.java
package interceptor;
import com.opensymphony.xwork2.ActionSupport;
public class PublicAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private String title;
private String content;
public void setTitle(String title){
this.title=title;
}
public String getTitle(){
return title;
}
public void setContent(String content){
this.content=content;
}
public String getContent(){
return content;
}
public String execute(){
return SUCCESS;
}
}
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 {
@Override
public String intercept(ActionInvocation ai) throws Exception {
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("讨厌", "喜欢");
ac.setContent(content);
}
if(content.contains("毛泽东")){
content=content.replaceAll("毛泽东", "毛主席");
ac.setContent(content);
}
//获取标题,并对特殊字眼进行转换后显示
String title=ac.getTitle();
if(title.contains("苹果")){
title=content.replaceAll("苹果", "水果");
ac.setTitle(title);
}
return ai.invoke();
}else{
return Action.LOGIN;
}
}
return Action.LOGIN;
}
}
以下是struts.xml的配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="interceptor" namespace="/" extends="struts-default">
<!-- 文件过滤拦截器配置,replace是拦截器的名字 -->
<interceptors>
<interceptor name="replace" class="interceptor.MyInterceptor"></interceptor>
</interceptors>
<action name="public" class="interceptor.PublicAction">
<result name="success">/success.jsp</result>
<result name="login">/news.jsp</result>
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="replace"/>
</action>
</package>
</struts>
相关文章推荐
- Java关键字——super、this
- XML——流机制解析器
- hdu acm hdu 2115 Java
- win 10系统安装myeclipse 9.1 破解问题
- spring mvc javaConfig版本搭建常见问题
- java内部类总结,举例
- XML——使用 XPath来定位信息+使用命名空间
- 运行java的class文件方法详解
- java数组复制
- 充满BUG的世界观——再遇Java内存泄露
- 【Eclipse】--Eclipse反编译插件jadeclipse的安装
- jsp中js获取java的传值
- java实现_用数组实现栈
- Spring 的优秀工具类盘点第 2 部分
- Spring 的优秀工具类盘点第 1 部分
- Java 泛型
- java web项目启动时自动加载自定义properties文件
- Spark1.5.2在eclipse生成jar提交到集群运行
- The Java™ Tutorials — Generics :Generics, Inheritance, and Subtypes 泛型,继承和子类型
- spring常用的工具类