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

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

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

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

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的报错。xml

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

三、找到问题了 对象

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

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

相关文章
相关标签/搜索