zoukankan      html  css  js  c++  java
  • (六)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。

  • 相关阅读:
    Coursera Algorithms Programming Assignment 2: Deque and Randomized Queue (100分)
    Coursera Algorithms week1 查并集 练习测验:3 Successor with delete
    Coursera Algorithms week1 查并集 练习测验:2 Union-find with specific canonical element
    项目选题报告答辩总结
    项目UML设计(团队)
    第七次作业--项目需求分析(团队)
    结对项目--第二次作业
    【软件工程实践 · 团队项目】 第二次作业
    第五次作业--原型设计(结对)
    团队项目系列博客 —— 在路上(之wampserver 修改根目录以及配置多站点以及修改端口号)
  • 原文地址:https://www.cnblogs.com/shyroke/p/6559401.html
Copyright © 2011-2022 走看看