The Struts framework provides two solutions for exception handling
- Declarative Exception Handling
-
Programmatic Exception Handling
- 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 <exception> tag is used to define the exception in the struts-config.xml file.
- The following are the attributes of the
<exception> tag
Key : The key defines the key present in MessageResources.properties file to describe the exception occurred.
Type : The class of the exception occurred.
Path : The page where the control is to be followed when an exception occurs.
Handler : The exception handler which will be called before passing the control to the file specified in path attribute.
- The Struts framework has a default exception-handler class that is used to process the exceptions if you do not configure one of your own.
- The default handler class isorg.apache.struts.action.ExceptionHandler
- The execute( ) method of this handler creates an ActionError, stores it into the proper scope, and returns an ActionForward object that is associated with the path attribute specified in the exception element.
- There are following two sections in struts-config.xml where the exceptions can be defined.
1. With Any Action mapping:
In this approach, the action class is identified which may throw an exception like password failure, resource access failure and so on.
<action
path="/login.do" type="com.javaskool.ExampleAction" parameter="method" input="/index.jsp">
<exception key="error.system" type="java.lang.RuntimeException" path="/index.jsp" />
</action>
Note:
-
Use Struts exception handler for generic exceptions
-
This specifies the path to which to forward when one of the specified exceptions occurs during the corresponding action invocation.
2. Defining exceptions globally:
If the application control is to pass on a single page for all similar type of exception then the exception can be defined globally. So if in any circumstance the exception occurs the control is passed globally to the exception and control is passed to the error page. This is done using the <global-exceptions> tag.
<global-exceptions>
<exception key="error.system" type="java.lang.RuntimeException" path="/index.jsp" />
</global-exceptions>
- In this approach the exceptions are caught using normal java language try/catch block and instead of showing the exception, some meaningful messages are displayed.
- In this approach the flow of control is also maintained by the program.
- The main drawback of the approach is the developer has to write the code for the flow of the application.
Creating Custom Exception Handlers in Struts
- Struts not only introduced the exception handling mechanism to pass the flow to the appropriate page automatically, it also gives flexibility to the programmers to create their own Exception Handlers.
- Struts provide the org.apache.struts.action.ExceptionHandler class for creating the custom exception handlers.
- All the custom Exception Handlers should extend the ExceptionHandler class and override the execute() method.
public class SpecialExceptionHandler extends ExceptionHandler
{
protected ActionForward execute(Exception ex, ExceptionConfig config, ActionMapping mapping, ActionForm formInstance, HttpServletRequest request,HttpServletResponse response) throws ServletException
{
ActionForward forward = null;
ActionError error = null;
String property = null;
if( ex instanceof BaseException)
{
BaseException baseException = (BaseException)ex;
String messageKey = baseException.getMessageKey( );
Object[] exArgs = baseException.getMessageArgs( );
if ( exArgs != null && exArgs.length > 0 ){
error = new ActionError( messageKey, exArgs );
}
else{
error = new ActionError( messageKey );
}
}
else{
error = new ActionError(config.getKey( ));
property = error.getKey( );
}
storeException(request, property, error, forward, config.getScope( ));
return forward;
}
- The specialized behavior that is performed by the custom handler is up to the programmer. The example given earlier, makes the error message more informative by inserting arguments into the ActionError.
- Developers can use both declarative and programmatic exception handling approaches while developing a Web application. When both are combined, then the Action classes will get the first opportunity to handle any exceptions.
- Only if an exception is not caught and handled by the Action class, will the declarative exception handling mechanism be used to process the error.
Recent Comments