|
Struts 1.3 provides a small, but effective exception-handling framework for the applications. There are two approaches available for the exception handling in struts.
- Declarative:- 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 in case exception occurred.
- Handler:- The exception handler which will be called before passing the control to the file specified in path attribute
There are following two sections in struts-config.xml where you can define the exceptions.
- With Any Action mapping:- In this approach the action class is identified which may throw the exception like password failure, resource access etc. For example:-
<action path="/exception" type="com.visualbuilder.ExampleAction" parameter="method" input="/index.jsp" > <exception key="error.system" type="java.lang.RuntimeException" path="/index.jsp" /> </action>
- Defining the Exceptions Globally for the struts-config.xml :- 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. For Example:-
<global-exceptions> <exception key="error.system" type="java.lang.RuntimeException" path="/index.jsp" /> </global-exceptions>
- Programmatic: - 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 programs. The main drawback of the approach is the developer has to write the code for the flow of the application.
|