web.xml中两种配置error-page的方法

本文介绍了在web.xml中配置error-page的两种方法:一种是根据HTTP错误码进行配置,另一种是根据Java异常类型进行配置,并提供了具体的XML配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我知道在web.xml中有两种配置error-page的方法,一是通过错误码来配置,而是通过异常的类型来配置,分别举例如下:

一.   通过错误码来配置error-page

Eg.

<error-page>
        <error-code>500</error-code>
        <location>/error.jsp</location>
  </error-page>

上面配置了当系统发生500错误(即服务器内部错误)时,跳转到错误处理页面error.jsp。

 

二.   通过异常的类型配置error-page

Eg.

<error-page>
        <exception-type>java.lang.NullException</exception-type>
        <location>/error.jsp</location>
   </error-page>

上面配置了当系统发生java.lang.NullException(即空指针异常)时,跳转到错误处理页面error.jsp

<think>我们根据用户需求调整web.xml文件,重点在于:1.保留原有架构和路径参数2.确保contextConfigLocation包含指定的三个配置文件:'spring-common-config.xml','spring-security-config.xml','quartz-config.xml'3.其他配置如欢迎页面、过滤器、Servlet、错误页面、安全约束等保持原样或适当补充根据引用[2]和[4],我们可以将多个配置文件用逗号分隔,放在context-param的param-value中。注意:用户提供的引用中,contextConfigLocation的配置方式有两种,一种是使用classpath*:加上路径,另一种是直接classpath:。这里我们按照用户上一次的示例,使用classpath:,并且不加通配符(因为用户明确指定了文件名)。因此,我们将context-param部分调整为:<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-common-config.xml,classpath:spring-security-config.xml,classpath:quartz-config.xml</param-value></context-param>同时,保留其他配置(如监听器、过滤器、Servlet等)并保持原样。另外,用户引用的内容中提到有多个配置文件,但用户指定了这三个,所以我们就用这三个。下面给出完整的web.xml,其中contextConfigLocation按上述要求修改,其他部分参考用户上一次的示例,并保持结构完整。</think> ### 优化后的完整 `web.xml` 配置文件 根据需求保留原有路径参数,同时确保 `contextConfigLocation` 包含指定文件,优化后配置如下: ```xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <!-- 应用显示名称 --> <display-name>Enterprise Application</display-name> <!-- 全局上下文参数 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:spring-common-config.xml, classpath:spring-security-config.xml, classpath:quartz-config.xml </param-value> </context-param> <!-- Spring 上下文监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 欢迎文件 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list> <!-- 字符编码过滤器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring MVC 前端控制器 --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-mvc.xml</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> <!-- 会话配置 --> <session-config> <session-timeout>30</session-timeout> </session-config> <!-- 错误页面 --> <error-page> <error-code>404</error-code> <location>/error/404</location> </error-page> <error-page> <error-code>500</error-code> <location>/error/500</location> </error-page> </web-app> ``` ### 关键优化点说明 1. **核心配置保留** 确保 `contextConfigLocation` 包含三个指定配置文件[^2][^4]: ```xml <param-value> classpath:spring-common-config.xml, classpath:spring-security-config.xml, classpath:quartz-config.xml </param-value> ``` 2. **配置文件加载顺序** 通过 `ContextLoaderListener` 按顺序加载指定配置文件,保持原有配置路径不变[^4]: ```xml <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> ``` 3. **多文件格式支持** 使用逗号分隔符支持多配置文件加载,适应模块化配置需求[^2]: ```xml classpath:spring-common-config.xml, classpath:spring-security-config.xml, classpath:quartz-config.xml ``` 4. **架构兼容性** 保持与原有分层配置方案的兼容性: - Spring MVC 配置:`/WEB-INF/spring-mvc.xml` - Service/DAO 层配置:通过 `classpath:*` 加载(示例中未展示)[^3] - Quartz 定时任务:`quartz-config.xml` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值