Struts2拦截器案例–文字过滤器的设计和应用
本文最后更新于 1521 天前,其中的信息可能已经有所发展或是发生改变。

1.设计评论页面。
news.jsp
[sourcecode language=”html” title=”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>评论</title>
</head>
<body>
请发表你的评论!<hr>
<s:form action="public" method="post">
<s:textfield name="title" label="评论标题" maxLength="36" />
<s:textarea name="content" cols="36" rows="6" label="评论内容"> </s:textarea>
<s:submit value="提交"/>
</s:form>
</body>
</html>[/sourcecode]
2.编写评论成功页面。
success.jsp
[sourcecode language=”html” title=”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>评论成功</title>
</head>
<body>
评论如下:<hr>
评论标题:<s:property value="title"/><br>
评论内容:<s:property value="content"/>
</body>
</html>[/sourcecode]
3.评论页面对应的业务控制器。
action/PublicAction.java
[sourcecode language=”java” title=”action/PublicAction.java”]
package action;

import com.opensymphony.xwork2.ActionSupport;

public class PublicAction extends ActionSupport {
private static final long serialVersionUID =1L;
private String title;
private String content;
public String execute(){return SUCCESS;}
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;
}

}[/sourcecode]
4.编写自定义拦截器。
interceptor/MyInterceptor.java
[sourcecode language=”java” title=”interceptor/MyInterceptor.java”]
import java.util.Map;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class MyInterceptor extends AbstractInterceptor {
private static final long serialVersionUID =1L;
public String intercept(ActionInvocation ai) throws Exception{
//获取页面提交的所有属性及属性值
Map<String,Object> parameters = ai.getInvocationContext().getParameters();
//对每对属性、属性值分别进行过滤,将过滤后的内容再保存到该属性中
for(String key:parameters.keySet()){
Object value = parameters.get(key);
if(value!= null && value instanceof String[]){
//instanceof 判断value是否为String[]类型
String[] valueArray = (String[]) value;
for(int i=0;i<valueArray.length;i++){
if(valueArray[i]!=null){
//判断用户提交的评论内容是否有要过滤的内容
if(valueArray[i].contains("讨厌")){
//以“喜欢”代替要过滤的内容“讨厌”
valueArray[i] = valueArray[i].replaceAll("讨厌", "喜欢");
//把替换后的评论内容设置为Action的评论内容
parameters.put(key, valueArray);
}
}
}
}
}
return ai.invoke();
}
}
[/sourcecode]
5.配置xml。
web.xml
[sourcecode language=”xml” title=”web.xml”]
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>benzhu1</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<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> [/sourcecode]
struts.xml
[sourcecode language=”xml” title=”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="default" namespace="/" extends="struts-default">
<interceptors>
<!– 文字过滤拦截器配置,replace是拦截器的名称 –>
<interceptor name="replace" class="interceptor.MyInterceptor" />
</interceptors>

<action name="public" class="action.PublicAction">
<!– 文字过滤Action配置 –>
<result name="success">/success.jsp</result>
<result name="login">/success.jsp</result>
<interceptor-ref name="replace"/> <!– 使用自定义拦截器 –>
<interceptor-ref name="defaultStack"/> <!– Struts2系统默认拦截器 –>
</action>
</package>
</struts>[/sourcecode]

项目下载:[bdbtn]https://pan.benzhu.xyz/%E4%BB%A3%E7%A0%81/%E6%BA%90%E4%BB%A3%E7%A0%81/%E9%A1%B9%E7%9B%AE/Struts2%E6%8B%A6%E6%88%AA%E5%99%A8%E6%A1%88%E4%BE%8B–%E6%96%87%E5%AD%97%E8%BF%87%E6%BB%A4%E5%99%A8%E7%9A%84%E8%AE%BE%E8%AE%A1%E5%92%8C%E5%BA%94%E7%94%A8.rar[/bdbtn]

效果图:

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇