How exception handling is provided in struts?

Answer Posted / qim2010

There are two approaches available for the exception
handling in struts.

Declarative:

Exceptions are defined in the struts-config.xml file and if
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.For
Example : (( If RuntimeException in SaveEmpAaction class ,
control goes to exception.jsp)
<action path="/saveEmp"
type="com.techfaq.SaveEmpAaction"
input="/empform.jsp" >
<exception key="error.system"
type="java.lang.RuntimeException" path="/exception.jsp" />
</action>

Where
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

OR

Defining the Exceptions Globally for the struts-config.xml :
( If RuntimeException in any Action class , control goes to
exception.jsp)
<global-exceptions>
<exception key="error.system"
type="java.lang.RuntimeException" path="/exception.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.

Is This Answer Correct ?    4 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Why was reload removed from struts (since 1.1)?

546


What should be the name of xml file used for validation in struts?

541


Define struts?

624


How long do struts last?

530


What is http redirect?

569






What are inner class and anonymous class?

547


What is the purpose of @element?

582


How to use dispatchaction?

579


What is the use of form bean in struts?

509


What is life cycle of an interceptor?

548


What is the role of action class in struts?

561


What are the core components of a struct2 based application?

564


Why we use struts over servlets?

524


Which configuration files are used in struts?

527


What do you mean by inner class and anonymous class?

597