1. 程式人生 > >HTTP Status 404 - No result defined for action xx.xx.xxAction and result error

HTTP Status 404 - No result defined for action xx.xx.xxAction and result error

今天整合一個ssh框架,發現報出這個錯誤,整了幾個鍾,都沒發現有任何錯誤。因為報錯大概意思是說沒有為web層的action類宣告一個result返回型別,但是我在struts.xml裡明顯寫了

<action name="studentAction_*" class="studentAction" method="{1}" >
	<result name="list">/jsp/student/list.jsp</result>
</action>

有的啊,有木有,後面索性把<result name="error">/error.jsp</result>也給寫上,結果發現能進入error介面,這就奇怪了,報錯說我沒宣告一個result返回值,但是我有,回去檢視action類我也寫了return "list";,我還睜大眼睛前後看了不下20次。。。

public String list() throws Exception {
		//封裝離線查詢物件
		DetachedCriteria dc = DetachedCriteria.forClass(Student.class);
		//判斷接受值不為空並封裝引數
		if(StringUtils.isNotBlank(student.getStud_name())){
			dc.add(Restrictions.like("stud_name", "%"+student.getStud_name()+"%"));
		}
		//呼叫Service物件ss查詢分頁資料
		Page pg = ss.getPage(dc,currentPage,pageSize);
		//將Page壓入request域,並轉發至列表屆面顯示
		ActionContext.getContext().put("Page", pg);
		return "list";
	}
完全沒錯的,action和struts.xml配置都沒有錯,然後檢視applicationContext.xml檔案內也建立了xxAction類,如果沒建立肯定報錯的啊。

<bean name="studentAction" class="com.mylea.web.action.StudentAction" scope="prototype" >
	<property name="ss" ref="studentService" ></property>
</bean>

struts也的確把action的建立交給了spring

<constant name="struts.objectFactory" value="spring"></constant>

等等,我測試error的時候,是可行的,沒有報錯,說明我的bean建立肯定沒問題,然後返回了error說明struts配置也肯定沒問題的,上面這些檢視程式碼有沒有寫錯其實都是白費功夫,根本沒有問題。

所以問題肯定是發生處理邏輯內部。然後層層尋找,可以線在action層syso一個變數,例如pg,如果沒有打印出來,拿肯定在中間發生了異常。其實用debug好使,後悔沒早用,最後發現我的Page物件(用來分頁顯示的),在計算頁數總頁數this.totalpage = (this.totalcount+this.pagesize-1)/this.pagesize;中,pagesize竟然為“0“,也不知道為什麼控制檯沒有報錯。正常來說這是RuntimeException應該會自動終止程式報錯才對啊。

血的教訓,一定要養成良好的程式設計習慣,在可能出錯的地方一定要拋異常,其次是我引發上述算術異常是因為int型別和integer型別混用,沒有統一,導致前面的if(pagesiez==null)這個地方沒有進去,因為int型別初始值為0,integer型別初始值為null,0當然不等於null,所以導致後面出現了除數為0的錯誤!

以上為個人遇到的情況,可能不符合其他人報出這個錯誤的原因,不過檢查方法應該通用:

1.按照上面方法檢查一下配置,確定無誤,那麼問題發生在執行中

2.可使用debug模式測試,檢查出變數異常的地方,然後修改即可