自定义HTTP错误404 - 基于注释的设置Spring 3.2

时间:2013-03-26 12:31:07

标签: spring spring-mvc http-status-code-404 http-error

是否可以使用Spring 3.2.1实现基于自定义HTTP 404错误页面的注释? 我在各种论坛上寻找方法,但找不到任何明确的答案。

我也尝试使用web.xml进行配置,但是在访问未映射的URL时它无法正常工作。 有什么帮助吗?

记录输出

  7259 [DEBUG] org.springframework.web.servlet.DispatcherServlet  - DispatcherServlet with name 'spring-test' processing GET request for [/spring-test/ss]
  7261 [DEBUG] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping  - Looking up handler method for path /ss
  7262 [DEBUG] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping  - Did not find handler method for [/ss]
  7262 [WARN ] org.springframework.web.servlet.PageNotFound  - No mapping found for HTTP request with URI [/spring-test/ss] in DispatcherServlet with name 'spring-test'
  7262 [DEBUG] org.springframework.web.servlet.DispatcherServlet  - Successfully completed request

的web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="rest" version="3.0" metadata-complete="true">

    <!-- The definition of the Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>main.java.net.bornil.config</param-value>
    </context-param>   

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>main.java.net.bornil.config</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <welcome-file-list>
        <welcome-file />
    </welcome-file-list>

    <error-page>
        <error-code>404</error-code>
        <location>/errors/404</location>
    </error-page>
</web-app>

控制器

@Controller
@RequestMapping(value = "/errors")
public class CommonExceptionHandler {

    private static Logger log = Logger.getLogger(CommonExceptionHandler.class.getName());

    @RequestMapping(method = RequestMethod.GET, value = "/{code}")
    public ModelAndView handleException(@PathVariable int code) {

        if (log.isDebugEnabled()) {
            log.debug("ERROR CODE IS: " + code);
        }

        return new ModelAndView("errors/404");
    }

}

1 个答案:

答案 0 :(得分:0)

我有同样的问题。我有一个简单的webapp,单页,在Spring论坛上度过了一个早晨(很好,你不能搜索'404',因为它只有3个字符长)和Google这是我发现的最佳解决方案。 假设您有index.jsp404.jsp,我的@Configuration'有:

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("index");
    registry.addViewController("/*").setViewName("404");
}