Struts2之拦截技术(附案例代码)

例题:将输入内容中的讨厌变成喜欢

在这里插入图片描述

1. 配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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">

	<display-name>Struts Blank</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>

2. action类

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 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(){
		if (title.contains("讨厌")) {			
			title =title.replaceAll("讨厌", "喜欢");
			}
		if (content.contains("讨厌")) {			
			content =content.replaceAll("讨厌", "喜欢");
			}
		return SUCCESS;
	}
}

3.编写拦截器

package interceptor;

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;

	@Override
	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[]) {
				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();// 进行执行下一个拦截器或Action
	}
}

4. 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>
	<constant name="struts.enable.DynamicMethodInvocation" value="false" />
	<constant name="struts.devMode" value="true" />	
	<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>

5. 页面

<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<title>评论</title>
</head>
<body>
	请发表你的评论!
	<hr>
	<form action="public" method="post">
	评论标题: <input  name="title" maxLength="36" /><br>
	评论内容:  <textarea name="content" cols="36" rows="6"></textarea>	
	<br>
	<input type="submit" value="提交" />
	</form>
</body>
</html>
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
    <head> <title>评论成功</title> </head>
    <body>
        评论如下:<hr>
        评论标题2:${title} <br>
        评论内容2:${content}
  </body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

莫余

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值