struts2自定义拦截器之过滤不良言论---http500可能的问题所在

刚开始学拦截器照着课本打,刚开始

1、Map<String,Object>……这里一直报错我怀疑是使用最新struts(struts2 5.3),标准已经改变,如果是其他原因我真是不知道了,这一句话在同学(struts2 3.*)的电脑上跑的很好。

2、课本行不通,我看了《java web 技术整合应用于项目实践》关于自定义拦截器这一章,也是照着写的,以下是自定义拦截器的代码:

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();
				String title=ac.getTitle();
				System.out.println(content);
				if(content.contains("草")||title.contains("草")){
					//代替
					content=content.replaceAll("草", "*");
					title=title.replaceAll("草", "*");
					//把替代后的评论内容设置为Action的评论内容
					ac.setContent(content);
					ac.setTitle(title);
				}
				//对象不空,继续执行
				return ai.invoke();
			}
			else{
				//返回Action中的LOGIN逻辑视图字符串
				return Action.LOGIN;
				
			}
			
		}
		return Action.LOGIN;
	}

  看起来比课本上的简单多了不是吗,但是运行出现HTTP 500的报错。

String content=ac.getContent(); 这一句取到的是null

3、找到问题了 

<interceptors>
<!-- replace是拦截器的名字 -->
<interceptor name="replace" class="interceptor.MyInterceptor"></interceptor>
</interceptors>

这个 要在struts.xml里(我是把这句放在了struts-***里,然后在struts.xml里include了一下)

原文地址:https://www.cnblogs.com/Amy-is-a-fish-yeah/p/7686146.html