Error handling using spring + servlet specification

I have a web application (servlet specification 2.5) with a spring dispatcherservlet dispatcher handling all incoming / errors / * and an error page configured to route to / error / something like this:

<servlet>
    <servlet-name>errorServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>errorServlet</servlet-name>
    <url-pattern>/erorr/*</url-pattern>
</servlet-mapping>
<error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/erorr/</location>
</error-page>

and errorServlet-servelt.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:annotation-config/>
    <context:component-scan base-package="some.base.package"/>
    <bean id="simpleUrlController" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/*">errorController</prop>
            </props>
        </property>
    </bean>
    <bean id="errorController" class="ErrorController">
        <property name="formView" value="formView"/>
        <property name="commandClass" value="Error"/>
        <property name="commandName" value="errorNAMe"/>
    </bean>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

Spots I need help:

  • This is the best approach to eliminate errors.
  • I know that there is a SimpleMappingExceptionResolver that I can declare in my configuration ... but I read somewhere that this class is only good with exceptions coming from spring controllers, and not with others.
+3
source share
1 answer
  • , , , , .
  • , . -, , , , Spring. , .

; , : http://developingdeveloper.wordpress.com/2008/03/09/handling-exceptions-in-spring-mvc-part-2/

: , , , . , StackOverflow, .

+5

Source: https://habr.com/ru/post/1713619/


All Articles