zno2

spring mvc handle 404

http://stackoverflow.com/questions/18322279/spring-mvc-spring-security-and-error-handling

 

正常情况下 no mapping found 不抛出异常

可以配置

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/web/applicationContext-servlet.xml</param-value>
        </init-param>
        <init-param>
            <param-name>throwExceptionIfNoHandlerFound</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

 

然后使用 @ControllerAdvice

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.NoHandlerFoundException;

@ControllerAdvice
public class ExceptionController {

    @ExceptionHandler(value = NoHandlerFoundException.class)  
    public String handleNoHandlerFoundException()  {  
        return "404";  
    }

}

 

posted on 2023-06-01 17:01  zno2  阅读(14)  评论(0编辑  收藏  举报

导航