• (六)Struts的简单异常处理


    一、异常的分类

     1.1  struts中的异常概念

    • Struts的声明式异常: 不处理异常,将异常交给struts框架来处理。

     1.2 局部异常

    • 局部异常:异常定义在Action里,异常处理只在这个Action中有效,其他action如果出现了异常则无法处理到。
    • struts.xml 中《action》的子标签<exception-mapping result="" exception=""></exception-mapping>异常映射可以用来处理异常,如果产生异常可以将用户引导到异常处理页面。其中result表示异常处理页面的名称,与result标签一起用。exception表示异常的类型,比如java.io.FileNotFoundException这是文件没有找到时的异常类型。
    • 示例:
        <package name="default" namespace="/exception" extends="struts-default">
            <action name="exception_1" class="action.ErrorAction">
                <result name="index_2">/index_2.jsp</result>
                
                <exception-mapping result="error_2" exception="java.io.FileNotFoundException"></exception-mapping>
                <result name="error_1">/error_1.jsp</result>
        
            </action>
        </package>

     解析: 当URL=“工程名/exception/exception_1”的时候,执行ErrorAction类对象,如果这个Action正确执行,则跳转到index_2.jsp这个页面,如果出现了文件没有找到异常(java.io.FileNotFoundException),就把跳转到<result>标签中的的error_1.jsp页面。

    • 一般来说,如果页面可能产生的异常比较多的时候,在struts.xml中异常映射就需要定义很多个,此时可以先定义几个具体的异常,然后对于其他异常可以合在一起处理,即

    <exception-mapping result="error_common" exception="java.lang.Exception"></exception-mapping>

    • 示例:
     <package name="default" namespace="/exception" extends="struts-default">
            <action name="exception_1" class="action.ErrorAction">
                <result name="index_2">/index_2.jsp</result>
                
                <exception-mapping result="error_2" exception="java.io.FileNotFoundException"></exception-mapping>
                <exception-mapping result="error_1" exception="java.lang.ArithmeticException"></exception-mapping>
                <exception-mapping result="error_common" exception="java.lang.Exception"></exception-mapping>
    <result name="error_1">/error_1.jsp</result> <result name="error_2">/error_2.jsp</result> <result name="error_common">/common.jsp</result> </action> </package>

    解析:  如果Action出现java.io.FileNotFoundException或者java.lang.ArithmeticException错误,就会跳转到相应的页面,如果不是这两个异常,则跳转到common.jsp页面。

    •  注意: 如果Action里有多个异常,那么当发现第一个异常的时候,就会跳转异常处理页面,这个Action接下去的异常将不会处理。

      1.2  全局异常

    • 定义在package里,使用<global-exception-mappings>,这个package里的所有action都可以使用。其他包要想使用,继承这个包即可。
       <package name="default" namespace="/exception" extends="struts-default">
                <global-results>
                 <result name="error_1">/error_1.jsp</result>
                <result name="error_2">/error_2.jsp</result>
                <result name="error_common">/common.jsp</result>
            </global-results>
            
            <global-exception-mappings>
                <exception-mapping result="error_2" exception="java.io.FileNotFoundException"></exception-mapping>
                <exception-mapping result="error_1" exception="java.lang.ArithmeticException"></exception-mapping>
                <exception-mapping result="error_common" exception="java.lang.Exception"></exception-mapping>
            </global-exception-mappings>
            
            <action name="exception_1" class="action.ErrorAction">
                <result name="index_2">/index_2.jsp</result>
            </action>
        </package>
    • 注意:1.<global-exception-mappings> 标签里不能定义<result name=""></result>

          2.<global-results> 要定义在<global-exception-mappings>之前。

          3.<global-exception-mappings>里的<result>对应的是<global-results>里的<result>里的name。

  • 相关阅读:
    Echarts 中当y轴有负数时,让x轴下落在y轴最小值(转载)
    echarts中如何去掉柱状图的横线和竖线(转载)
    echarts 风向 风速曲线
    echarts legend.type scroll 显示问题;渲染错误
    css实现左侧固定宽度,右侧宽度自适应(转载)
    npm 如何查看一个包的版本信息?(转载)
    echart series areaStyle 颜色不显示
    Spark与Hadoop Shuffle对比
    hive中with...as...的用法
    [转]MyBatis
  • 原文地址:https://www.cnblogs.com/shyroke/p/6559401.html
Copyright © 2020-2023  润新知