404和500错误页的配置

  1. 我们在webap/views/commons/error(目录自己定)新建我们自定义的错误页面,404.html, 500.html等等。

  2. SimpleMappingExceptionResolver只实现映射关系,我们还需要通过配置web.xml来实现。在欢迎页后面添加:

    
        404
        /error/404.html
    
    
    
        500
        /error/500.html
    

  3. 在spring-mvc配置文件中将404.html、500.html等设置为资源文件,避免被springmvc再次拦截。

    <mvc:resources mapping="/error/**" location="/webapp/views/commons/error/" />
  4. 配置SimpleMappingExceptionResolver。

    <bean class="org.springframework.web.servlet.handler. SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <map>
                <entry key="ResourceNotFoundException" value="common/error/resourceNotFoundError" />
                <entry key=".DataAccessException" value="common/error/dataAccessError" />
            map>
        property>
        <property name="statusCodes">
            <map>
                <entry key="common/error/resourceNotFoundError" value="404" />
                <entry key="common/error/dataAccessError" value="500" />
            map>
        property>
    bean> 

你可能感兴趣的:(java,Java整理)