Tuesday 29 November 2011

Spring MVC error page

It’s always recommended to display a custom friendly error page instead of the default long java plain exception code in tomcat.So what do you do in Spring MVC Web application? 1) Declare SimpleMappingExceptionResolver in Spring’s bean configuration file.
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
        <props>
            <prop key="java.lang.Exception">error</prop>
        </props>
    </property>
</bean>
2) Define "error.jsp"
<html>
<head>
    <title>Error</title>
    <link rel="stylesheet" href="<%=request.getContextPath()%>/styles/smoothness/jquery-ui-1.8.12.custom.css" type="text/css"/>
    <script type="text/javascript" src="<%=request.getContextPath()%>/scripts/jquery-1.5.1.min.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath()%>/scripts/jquery-ui-1.8.12.custom.min.js"></script>
</head>
<body>
    <div>
        <div class="ui-widget">
            <div class="ui-state-error ui-corner-all" style="padding: 0 .7em;">
                <p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span>
                    <strong>Alert:</strong> An error has occurred. Please contact our administrator for details.</p>
                <p><strong>Cause:</strong></p> ${exception.message}
            </div>
        </div>
    </div>
</body>
</html>
3) Exmaple

No comments:

Post a Comment