zoukankan      html  css  js  c++  java
  • Struts2异常处理

    Struts2提供了一种声明式的异常处理方式

    1、在Struts.xml中配置<exception-mapping .../>,包含两个属性:
    exception:此属性指定该异常映射所设置的异常类型
    result:此属性指定所指向的结果

    2、异常映射分为两种:
    局部异常映射:<exception-mapping .../>作为<action .../>子元素
    全局异常映射:<exception-mapping .../>作为<global-exception-mappings>子元素


    3、重写execute()方法时要抛出所有异常类型
    public String execute() throws Exception{}

    4、配置文件
    <package name="gao" extends="struts-defeault">
    <global-results>
    <result name="sql">/exception.jsp</result>
    <result name="root">/exception.jsp</result>
    </global-results>
    <!--定义全局异常映射-->
    <global-exception-mappings>
    <!--Action抛出SQLException异常时,转入名为sql的结果-->
    <exception-mapping exception="java.sql.SQLException" result="sql">
    <!--Action抛出Exception异常时,转入名为root的结果-->
    <exception-mapping exception="java.lang.Exception" result="root">
    </global-exception-mappings>
    <action name="login" class="gao.LoginAction">
    <exception-mapping exception="gao.MyException" result="my" />
    <result name="my">/exception.jsp</result>
    <result name="error">/error.jsp</result>
    <result name="success">/welcome.jsp</result>
    </action>
    </package>

    5、输出异常信息
    <s:property value="exception"/>输出异常对象本身
    <s:property value="exception.message"/>
    <s:property value="exceptionStack"/>输出异常堆栈信息

  • 相关阅读:
    使用InstelliJ IDEA创建Web应用程序
    别了WindowsXP
    在MyEclipse中搭建Spring MVC开发环境
    iPhone中国移动收不到彩信,联通不用设置都可以,具体设置方法:
    WebLogic 服务器配置
    c# 第五课 string
    c# 第五课 regex
    c# 第四课 Arrays
    c# 第四课 interfaces
    c# 第五课 async await
  • 原文地址:https://www.cnblogs.com/ikuman/p/2244591.html
Copyright © 2011-2022 走看看