---
title: "Struts 1.1  ExceptionHandler（转）"
author: "desert3"
date: "2012-11-02T10:20:43Z"
tags: []
categories: ["企业架构"]
slug: "struts-1-1-exceptionhandler"
draft: true
lastmod: "2012-11-02T10:22:41Z"
id: 1714232
url: "https://desert3.iteye.com/blog/1714232"
---

转自：[SSH项目中利用ExceptionHandler处理异常](http://fengzhiyin.iteye.com/blog/432182)
[Class ExceptionHandler](http://struts.apache.org/1.2.9/api/org/apache/struts/action/ExceptionHandler.html)： An ExceptionHandler is configured in the Struts configuration file to handle a specific type of exception thrown by an Action.execute method.

**一、概述**
在Struts1.1的版本中加入了对异常的处理 Exception Handling，有了它就不需要我们用try/catch等捕获异常，一旦出现了我们已经定义的异常那么就会转到相应得页面，并且携带定制的信息。Struts框架提供了默认的异常处理org.apache.struts.action.ExceptionHandler，它的<span style="color:blue">execute()</span>方法负责处理异常。在需要实现自定义处理时重写方法，可以在配置文件定义由谁来处理Action类中掷出的某种异常。

**二、Struts框架处理异常的流程**
struts的<span style="color:red">控制器负责捕获各种异常</span>，包括控制器运行中本身抛出的异常，以及调用模型的业务方法时抛出的异常。当struts的控制器捕获到异常后，在异常处理代码块中，创建描述信息的actionMessage对象把它保存在acionMessages(或其子类actionErrors)对象中，然后把actionMessage保存在特定范围（配置文件中的scope）。然后可以用<span style="color:blue"><html:errors /></span>检索特定范围内的actionMessages对象

**三、自定义异常处理**
1， 创建自己的异常处理类public class ExceptionHandler extends org.apache.struts.action.ExceptionHandler
2， 定义异常处理配置文件 
<span style="color:red">全局异常 </span>在struts-config.xml中定义<global-exceptions />

```xml

    <global-exceptions><!-- 元素可以包含一个或者多个<exception> -->  
        <exception   
            key="error.common"<!-- <message-resources parameter="MessageResources" />中的key -->  
            type="com.fengzhiyin.exception.ExistException"<!-- 指定需要处理异常类的名字 -->  
            handler="com.bjnv.water.common.ExceptionHandler" <!-- 指定异常处理类默认是ExceptionHandler -->  
            path="/jsp/common/error.jsp"<!-- 指定当前异常发生的时候转发的路径 -->  
            scope="request"><!-- 指定ActionMessages实例存放的范围 -->  
        </exception>  
    </global-exceptions>  

```

<span style="color:red">局部异常 </span>在struts-config.xml中定义<exception />

```xml

    <action-mappings>  
    　　<action path=”/waterUser”  
    　　　　    type=”**Action”  
    　　　　    name=”*Form”>  
    　　  <exception key=”<span style="font-size: x-small;">expired.existName</span>”  
    　　      type=” com.fengzhiyin.exception.ExistException”  
    　　      path=”/error.jsp”/>  
    　　　　    <forward name=”success” path=”***”/>  
    　　</action>  
    </action-mappings>  

```

3， 创建异常信息显示页面

```html

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>  
　　<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>  
　　<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>  
　　<html:html locale="true">  
　　<head>  
　　 <title> Exception Handler</title>  
　　 <html:base />  
　　 </head>  
　　 <body>  
　　 <h1>系统出现一个意外</h1>  
　　 请将下面的提示信息反馈给你的系统管理员:<br>  
　　 <html:errors /> <!--将在这里显示”<span style="font-size: x-small; color: #0000ff;">你要添加的用户名称已经存在，请添加新的名称！</span>”-->  
　　 </body>  
　　</html:html> 

```

4， 创建测试action

```java
    public class **Action extends BaseAction {  
    　　 public ActionForward execute(ActionMapping mapping,  
    　　  ActionForm form,  
    　　  HttpServletRequest request,  
    　　  HttpServletResponse response)throws Exception {  
    　　   throw com.fengzhiyin.exception.ExistException();  
    　　 }  
    }  
```