您的位置:首页 > 编程语言 > Java开发

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: