Declarative Exception Handling in Struts

Declarative Exception Handling is the procedure of handling Exceptions declaratively through the help of xml files. It is not needed to include exception-handling code in the java code of the application. The way of handling the exception would be specified in the xml file. In case of struts it is specified in the struts-config.xml file. The benefit of Declarative Exception Handling is if ever there is need to change the exception handling mechanism, changes can be made to the xml file, without the need to recompile the java code. The exception handling mechanism is decoupled from the rest of the application, which is very important property of a good software design.

If one uses the old exception handling mechanism there would be a lot of exception handling code duplication. For example if 40 to 50 struts Action subclasses use a business logic that has a method that throws some exception, then there will be a lot of exception handling code duplicated in all these Action subclasses. If ever there is need to make some change in the exception handling code then it has to changed in all the Action subclasses. But in case of Declarative exception handling code changes can be easily done in one place and it will be visible everywhere.

Configuring Declarative Exception handling in Struts
Exception handler definition should be stated in the struts-config.xml as forward definitions are specified in it. Similar to forward definitions there are two type of exception handler definitions, they are global and local action specific exception handler definition. Global exception handlers are available to the whole application where as local action specific are available to that particular action.

E.g. of configuring

The code below should be included in the struts-config.xml for global exceptions.

<global-exceptions>
<exception
type=”mypack.NoResultFoundException”
key=”error.NoResultFoundException”
path=”/exception.jsp”/>
</global-exceptions>

  

Explanation:

“type”: holds the fully qualified class name of the exception that the handler will handle.

“key”: holds the key in the properties file that will help to produce the error message when this exception occurs.

“path” holds the page that will be opened when an exception occurs.

Below code should be included for action specific exception handling

<action-mappings>
<action path="/search"
type="myPack.SearchAction"
name="searchForm"
scope="request"
validate="true"
input="/search.jsp">
<exception
type="myPack.NoResultsFoundException"
key="error.NoResultsFoundException"
path="/exception.jsp"/>
</action>
</action-mappings>

  

Using the above technique

If there is a NoResultsFoundException type of object thrown outside SearchAction then only it cannot be caught by the handler. It can only be caught if it is thrown from inside the SearchAction. For Declarative exception handling the try and catch blocks that were used should be removed from the actions so that it will be handled by strutshandler.

Steps for creating a custom exception handler.
1) A new exception handler class is created
2) The definition of the new exception handler is added to the struts-config.xml.

Creating a new Exception Handler class.
A class should be created that extends the org.apache.struts.action.ExceptionHandler. The execute method should be overridden by functionality of our default handler.

class CustomHandler extends org.apache.struts.action.ExceptionHandler
{
ActionForward execute(Exception ex,ExceptionConfig config,ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)throws ServletException
{
// Code to send a mail to the admin regarding the exception;
return super.execute(ex,config,mapping,form,request,response);
}
}

  

Adding the definition of the new exception handler in the struts-config.xml.

<exception
type=”mypack.NoResultFoundException”
handler=”myPack.CustomHandler”
key=”error.NoResultFoundException”
path=”/exception.jsp”/>

  

时间: 2024-10-12 16:51:34

Declarative Exception Handling in Struts的相关文章

[转载]A Crash Course on the Depths of Win32 Structured Exception Handling

转自:[已完工][经典文章翻译]A Crash Course on the Depths of Win32 Structured Exception Handling 原文题目: <<A Crash Course on the Depths of Win32™ Structured Exception Handling>> 原文地址: http://www.microsoft.com/msj/0197/Exception/Exception.aspx 原作者: Matt Pietr

OAF_OAF Exception Handling异常处理(概念)

2014-06-12 BaoXinjian 一.摘要 Oracle Application Framework将异常分为三类 异常类型type 1. 常规异常General Exception Class:oracle.apps.fnd.framework.OAException Function: 提供了在运行时刻同时显示多种类型异常的手段,结合EBS的Message,可显示有用的信息 2. 验证异常Validation Exception Class:oracle.apps.fnd.fram

/EH (Exception Handling Model)

Specifies the kind of exception handling used by the compiler, when to optimize away exception checks, and whether to destroy C++ objects that go out of scope because of an exception. If /EH is not specified, the compiler catches both asynchronous st

Exception handling

https://en.wikipedia.org/wiki/Exception_handling In general, an exception breaks the normal flow of execution and executes a pre-registered exception handler. The details of how this is done depends on whether it is a hardware or software exception a

[转]java-Three Rules for Effective Exception Handling

主要讲java中处理异常的三个原则: 原文链接:https://today.java.net/pub/a/today/2003/12/04/exceptions.html Exceptions in Java provide a consistent mechanism for identifying and responding to error conditions. Effective exception handling   will make your programs more ro

Exception Handling in ASP.NET Web API webapi异常处理

原文:http://www.asp.net/web-api/overview/error-handling/exception-handling This article describes error and exception handling in ASP.NET Web API. HttpResponseException Exception Filters Registering Exception Filters HttpError HttpResponseException Wha

CoreCLR on Mac:体验managed exception handling

C#测试代码: using System; class Program { static void A() { try { Console.WriteLine("Throwing an exception"); throw new Exception("Did you catch it?"); } finally { Console.WriteLine("A.finally()"); } } static void B() { try { C()

SEH(structured exception handling)基础篇---终止处理程序

前序:最近看SEH看的头晕脑胀/(ㄒoㄒ)/~~,SEH最开始是Windows提供的异常处理机制,只是一个简单的框架,而我们现在使用的SEH都是编译器已经在系统提供的最基础的框架上做了修改的增强版(原始版比较原始,牵扯到大量Windows基础知识,并且需要反汇编看汇编代码来理解....本人现在功力较浅,需要慢慢消化,先看一下增强版,通过C和C++来接触一下. SEH包含两部分功能:终止处理(termination handling)和异常处理(exception handling).在这先讲一下

Exception Handling Statements (C# Reference)

Exception Handling Statements (C# Reference) C# provides built-in support for handling anomalous situations, known as exceptions, which may occur during the execution of your program. These exceptions are handled by code that is outside the normal fl