13.3、异常处理——DefaultHandlerExceptionResolver

当程序发生异常时,DefaultHandlerExceptionResolver异常解析器默认可以解析指定的异常,见下面的源码,默认可以解析HttpRequestMethodNotSupportedException、HttpMediaTypeNotSupportedException等异常类,当有这些类的异常发生时,默认交给DefaultHandlerExceptionResolver异常解析器进行解析,DispatcherServlet默认实现了DispatcherServlet 的Bean。

DefaultHandlerExceptionResolver源码

    @Override
    @Nullable
    protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response,
            @Nullable Object handler, Exception ex) {

        try {
            if (ex instanceof HttpRequestMethodNotSupportedException) {
                return handleHttpRequestMethodNotSupported((HttpRequestMethodNotSupportedException) ex, request,
                        response, handler);
            }
            else if (ex instanceof HttpMediaTypeNotSupportedException) {
                return handleHttpMediaTypeNotSupported((HttpMediaTypeNotSupportedException) ex, request, response,
                        handler);
            }
            else if (ex instanceof HttpMediaTypeNotAcceptableException) {
                return handleHttpMediaTypeNotAcceptable((HttpMediaTypeNotAcceptableException) ex, request, response,
                        handler);
            }
            else if (ex instanceof MissingPathVariableException) {
                return handleMissingPathVariable((MissingPathVariableException) ex, request,
                        response, handler);
            }
            else if (ex instanceof MissingServletRequestParameterException) {
                return handleMissingServletRequestParameter((MissingServletRequestParameterException) ex, request,
                        response, handler);
            }
            else if (ex instanceof ServletRequestBindingException) {
                return handleServletRequestBindingException((ServletRequestBindingException) ex, request, response,
                        handler);
            }
            else if (ex instanceof ConversionNotSupportedException) {
                return handleConversionNotSupported((ConversionNotSupportedException) ex, request, response, handler);
            }
            else if (ex instanceof TypeMismatchException) {
                return handleTypeMismatch((TypeMismatchException) ex, request, response, handler);
            }
            else if (ex instanceof HttpMessageNotReadableException) {
                return handleHttpMessageNotReadable((HttpMessageNotReadableException) ex, request, response, handler);
            }
            else if (ex instanceof HttpMessageNotWritableException) {
                return handleHttpMessageNotWritable((HttpMessageNotWritableException) ex, request, response, handler);
            }
            else if (ex instanceof MethodArgumentNotValidException) {
                return handleMethodArgumentNotValidException((MethodArgumentNotValidException) ex, request, response,
                        handler);
            }
            else if (ex instanceof MissingServletRequestPartException) {
                return handleMissingServletRequestPartException((MissingServletRequestPartException) ex, request,
                        response, handler);
            }
            else if (ex instanceof BindException) {
                return handleBindException((BindException) ex, request, response, handler);
            }
            else if (ex instanceof NoHandlerFoundException) {
                return handleNoHandlerFoundException((NoHandlerFoundException) ex, request, response, handler);
            }
            else if (ex instanceof AsyncRequestTimeoutException) {
                return handleAsyncRequestTimeoutException(
                        (AsyncRequestTimeoutException) ex, request, response, handler);
            }
        }
        catch (Exception handlerException) {
            if (logger.isWarnEnabled()) {
                logger.warn("Handling of [" + ex.getClass().getName() + "] resulted in Exception", handlerException);
            }
        }
        return null;
    }

下面以HttpRequestMethodNotSupportedException为例来测试,当发生HttpRequestMethodNotSupportedException异常时,会有DefaultHandlerExceptionResolver异常解析器进行处理。
1、web.xml和spring的配置文件
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">

    <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

spring的配置文件springmvc.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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <context:component-scan base-package="com.lzj.springmvc"></context:component-scan>
    <!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <mvc:annotation-driven></mvc:annotation-driven>
</beans>

2、创建请求
在WebContent下面创建index.jsp,内容为:

<a href="springMVC/testDefaultHandlerExceptionResolver">Test DefaultHandlerExceptionResolver</a>

3、创建请求控制器
控制器用于截获index.jsp中的请求

@Controller
@RequestMapping("/springMVC")
public class TestSpringMVC {

    //设置默认接收POST请求
    @RequestMapping(value="/testDefaultHandlerExceptionResolver", method=RequestMethod.POST)
    public String testDefaultHandlerExceptionResolver(){
        System.out.println("testDefaultHandlerExceptionResolver");
        return "success";
    }
}

当发送Index.jsp请求时,由于index.jsp中的超链接请求为GET请求,当控制器截获该请求后,由于控制器设置的默认接收POST请求,因此会抛出一个HttpRequestMethodNotSupportedException异常,该异常默认由DefaultHandlerExceptionResolver解析器进行解析,返回个客户端异常界面,如下图所示:
这里写图片描述
浏览器中显示不支持GET请求。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Xilinx ISE 13.3 是一款基于FPGA的设计软件,它允许用户使用硬件描述语言和图形化界面设计FPGA的逻辑电路。ISE 13.3 具有丰富的功能和性能,可以支持多种FPGA器件,并且具有良好的仿真和调试功能。 ISE 13.3 提供了完整的设计流程,从原型设计到最终的验证和调试,包括了项目管理、设计入门、合成、仿真和生成配置文件等多个功能。通过使用ISE 13.3,设计人员可以有效地优化设计流程和提高设计质量,从而使FPGA设计更加高效和可靠。 ISE 13.3 还支持多样化的设计开发语言和板级设计工具,如Verilog、VHDL、SystemC和MATLAB等,这些工具使得设计人员可以自由选择使用最适合自己的工具进行FPGA的设计开发。同时,ISE 13.3 还支持第三方EDA工具,包括Synopsys、Cadence和Mentor等,使设计人员可以更好地扩展和定制ISE 13.3 的功能。 总之,Xilinx ISE 13.3 是一款强大的FPGA设计软件,它提供了丰富的功能和性能,能够满足各类FPGA设计的需求。并且,它可以提高设计人员的工作效率,减少开发周期和成本,从而提高设计质量和竞争力。 ### 回答2: Xilinx ISE 13.3是一款FPGA设计和验证软件。它包含了一系列的工具,可以帮助开发人员进行可编程逻辑器件的设计和仿真。该软件支持大量的Xilinx FPGA,包括Spartan、Virtex和Kintex等系列。该软件能够提供从设计到实现的完整设计流程,包括设计入门、仿真、合成、实现和下载等一系列关键技术环节。通过Xilinx ISE 13.3,用户可以通过可视化的界面进行设计,也可以通过命令行的方式进行工程管理和设计流程的自动化。同时,该软件还支持Verilog、VHDL等常用的硬件描述语言,以及Vivado等后续版本的设计,可以满足不同开发人员的需求。作为FPGA设计和验证软件的领军者,Xilinx ISE 13.3为设计人员提供了完善的设计流程和优秀的产品支持,无疑是开发FPGA设计师们提高效率、提升设计质量的不二选择。 ### 回答3: Xilinx ISE13.3是一款综合设计工具,专为FPGA及CPLD设计而开发。它是Xilinx公司开发的一款功能强大、易于使用的软件,广泛用于数字电路设计,包括逻辑设计、验证、实现、仿真和调试等工作。ISE13.3支持包括Xilinx的Spartan、Virtex和Artix系列FPGA在内的多个FPGA家族,同时还支持查询Xilinx官方的库文件和第三方的软件库。 ISE13.3提供了许多有用的工具和功能,比如,它具有强大的仿真和调试工具,使设计师们能够更方便地检查其设计是否按计划实现;它还有许多跨平台的特点,例如可以在Microsoft Windows、Linux等多个操作系统平台上运行。除此之外,ISE13.3支持多种语言对设计进行描述,如VHDL、Verilog、SystemC等,使设计效率得以进一步提高。 但同时也有一些ISE13.3的不足之处,比如,其在安装和使用上可能存在一些困难,在使用时也需要具备一定基础的知识和经验,如果被使用不当就可能会导致系统的不稳定等问题,所以需要花费更多精力去学习和了解它的使用方法。 综上所述,ISE13.3是一款强大而实用的FPGA综合设计工具,具有许多优越的特点,但也需要注意其使用时的注意事项,尤其是在安装和使用上,需要专业人员进行操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值