[Spring MVC]-500/404 error handling

Source: Internet
Author: User

Spring MVC 404 Cannot find a page error can be configured directly using Web. XML:

Join in the <web-app/> node:

    < Error-page >        < Error-code >404</error-code>         < location >/web-inf/views/errors/404.jsp</location>    </  error-page>

500 run-time error, you can use the Simplemappingexceptionresolver configuration of Spring MVC:

    <!--Global Exception Configuration -    <BeanID= "Exceptionresolver"class= "Org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">        < Propertyname= "Exceptionmappings">            <Props>                <propKey= "Java.lang.Exception">errors/500</prop>                <propKey= "Java.lang.Throwable">errors/500</prop>            </Props>        </ Property>        < Propertyname= "Statuscodes">            <Props>                <propKey= "errors/500">500</prop>            </Props>        </ Property>        <!--set the log output level, do not define error log messages such as warnings by default -        < Propertyname= "Warnlogcategory"value= "WARN"></ Property>        <!--default error page, use this default configuration when you cannot find the corresponding view for the exception specified in mappings above -        < Propertyname= "Defaulterrorview"value= "errors/500"></ Property>        <!--default HTTP status code -        < Propertyname= "Defaultstatuscode"value= "$"></ Property>    </Bean>

The view JSP page that corresponds to the 500 error:

<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%><%@taglib prefix="C"URI="Http://java.sun.com/jsp/jstl/core"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Error</title></Head><Body>    <%Exception ex=(Exception) Request.getattribute ("Exception"); %>    <H2>Exception:<%=ex.getmessage ()%></H2>    <P/>    <%Ex.printstacktrace (NewJava.io.PrintWriter (out)); %></Body></HTML>

Test:

Alternatively, you can use inherited handlerexceptionresolver to handle 500 of errors.

Reference article references:

Http://www.cnblogs.com/xguo/p/3163519.html

[Spring MVC]-500/404 error handling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.