zoukankan      html  css  js  c++  java
  • 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.

    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

    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.

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

  • 相关阅读:
    RESTful日#2:使用Unity容器和引导程序在Web api中使用依赖注入实现控制反转
    RESTful日#2:使用Unity容器和引导程序在Web api中使用依赖注入实现控制反转
    带有可选选项的输入文本框(组合框)
    在组合框中嵌入一个DataGridView
    ItemData在。net
    实现一个所有者绘制的组合框
    模板化的“请等待”按钮和模板控件介绍
    使用AvalonEdit (WPF文本编辑器)
    办公风格的扁平组合箱
    【windows】telnet 和一些dos命令
  • 原文地址:https://www.cnblogs.com/hephec/p/4590613.html
Copyright © 2011-2022 走看看