Declarative Exception Handling in Struts

Exceptions in Struts based J2EE applications:

Usually there are two ways in which you can catch the exceptions.

  1. Declarative Exception Handling
  2. Programmatic Exception Handling

Programmatic Exception Handling

In this approach the exceptions are caught using normal java language try/catch block.In this approach the flow of control is also maintained by the programs. The main drawback of the approach is the developer has to write the code for the flow of the application.

In this way, we need to put code in try-catch block. If the exception is caught in the try block, the control flows into catch block, where we add the errors into the messages and put it in the session. Then the control is forwarded manually to the error-page where the warning message is displayed. The drawback of this method, is the developer is fully responsible for the various flow of applications where the chance of misleading is high.

Exceptions in Struts based J2EE applications:

Usually there are two ways in which you can catch the exceptions.

  1. Declarative Exception Handling
  2. Programmatic Exception Handling

Programmatic Exception Handling

In this approach the exceptions are caught using normal java language try/catch block.In this approach the flow of control is also maintained by the programs. The main drawback of the approach is the developer has to write the code for the flow of the application.

In this way, we need to put code in try-catch block. If the exception is caught in the try block, the control flows into catch block, where we add the errors into the messages and put it in the session. Then the control is forwarded manually to the error-page where the warning message is displayed. The drawback of this method, is the developer is fully responsible for the various flow of applications where the chance of misleading is high.






Declarative Exception Handling

Declarative Exception Handling is the way of handling Exceptions with the help of xml files. In this way of handling exception there is no need to write exception-handling code in the application. In this approach the exceptions are defined in the struts-config file and in case of the exception occurs the control is automatically passed to the appropriate error page. The biggest benefit of Declarative Exception Handling is if there is requirement to change the exception handling mechanism, changes can be made to the xml file, without recompilation of java code.

The declarative exceptions can be placed as action-specific or global. In action specific, we define these exceptions and path inside the action tag. Otherwise, the exceptions are defined inside the global-exception tag. This would mean that, if this exception arises in any action class, it would automatically forwarded to the specified path known as error-page. The error-page then shows the exact warning message.


Benefit of Declarative Exception Handling

Prior to version 1.1, exception handling in Struts applications works same way as it does in most other applications. Inside your code, you have to write several try-catch blocks to handle each of the exceptions that could be thrown.If developer uses the old exception handling mechanism there would be code duplication. For example if 10 to 20 struts Action class implement a business logic that throws same exception, then there will be a lot of exception handling code duplicated in all these Action classes. If there is need to change exception handling logic , then same code changes should be made in all the Action classes. But in case of Declarative exception handling, code changes can be easily done in one place and it will be visible to all Action class.


How to configure Declarative Exception handling in Struts

Exception handler definition should be mentioned in the struts-config.xml.There are two types of exception handler definition global and local action specific exception handler definition. Global exception handlers are available to the whole application where as local action handlers are specific to that particular action.



Action specific exception handling

Below code should be included in the struts-config.xml for action specific exceptions.

Entry in struts-config.xml for action specific Declarative Exception handling:

<action path = "/login" type= "com.jwt.struts.LoginAction" 
parameter="name" input="/login.jsp" >
<exception key="error.invalid.Password" 
type="RuntimeException" path="/index.jsp" />
</action>

In the above code exceptions will be handled during the login action.

Entry in ApplicationResources.properties

error.invalidPassword = Invalid password entered. Please try agin.

Handling Exception globally

In this way exceptions should be defined inside the global-exception tag in the struts-config.xml. This would mean that, if this exception arises in any action class, it would automatically forwarded to the specified path known as error-page. The error-page then shows the exact warning message.Below code should be included in the struts-config.xml for global exceptions.

<global-exceptions>
<exception
type=”com.javawebtutor.NoResultFoundException”
key=”error.NoResultFound.Exception”
path=”/noresultpage.jsp”/>
</global-exceptions>

In the above code

  • type: holds the fully qualified class name of the exception that the handler will handle.
  • key: holds the key in the properties file about error message when this exception occurs.
  • path: holds the page that request will forwarded when an exception occurs.

Next Article: Custom Exception Handler in Struts




comments powered by Disqus