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了一下)字符串

相關文章
相關標籤/搜索