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.

  • 相关阅读:
    oracle无法使用sysdba的解决办法
    打开计算机管理的命令
    python 多进程详解(Multiprocessing模块)
    zabbix使用Percona数据库tokudb引擎
    python自定义异常类
    xml(打补丁)设置timestamp类型指定的默认值以及varchar2类型的默认值
    This inspection suggests to replace null check with Object/Stream static method
    后端Itext生成pdf到传到前端,支票打印A4纸
    easyexcel导入获取表头并且表头为不固定列
    9. 云停车(芊熠相机)
  • 原文地址:https://www.cnblogs.com/hephec/p/4590613.html
Copyright © 2011-2022 走看看