1. 程式人生 > >Spring MVC 404 Not Found 無錯誤日誌解決方案

Spring MVC 404 Not Found 無錯誤日誌解決方案

微笑場景描述,使用Spring MVC 框架,進行資料儲存,用firefox的firebug跟蹤發現404 Not Found。

 

 分析:後臺沒有列印任何錯誤日誌,無法分析問題所在。

解決方案(由我朋友提供)

在spring-mvc.xml 配置檔案中 加上一下紅色部分的配置檔案。


在controller 下面新增 ControllerAdviceTest.java 檔案(注意:必須是controller下面,因為在spring-mvc.xml 中配置了 component-scan)


再次執行 後臺輸出以下錯誤日誌


很明顯表明,物件名稱有問題,至此問題解決。附件:附上ControllerAdviceTest.java 原始碼(不能新增壓縮包嗎? 請自行修改bag名)

package com.snake.controller;

import org.apache.log4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.NativeWebRequest;

@ControllerAdvice
public class ControllerAdviceTest {

	protected static Logger logger = Logger.getLogger(ControllerAdviceTest.class);
	
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public String processUnauthenticatedException(NativeWebRequest request,Exception e) {
    	logger.error(e.getMessage());
        return "viewName"; //返回一個邏輯檢視名
    }
}