Google News
logo
Java Struts - Interview Questions
How can we handle exceptions thrown by application in Struts2?
Struts2 provides a very robust framework for exception handling. We can specify global results in packages and then map specific exceptions to these result pages. The exception mapping can be done at the global package level as well as the action level.
 
It’s a good idea to have exception result pages to provide some information to the user when some unexpected exception occurs that is not processed by the application. The sample configuration in the struts.xml file looks like below.

<package name="user" namespace="/" extends="struts-default">
 
<global-results>
    <result name="exception">/exception.jsp</result>
    <result name="runtime_exception">/runtime_exception.jsp</result>
    <result name="error">/error.jsp</result>
</global-results>
 
<global-exception-mappings>
    <exception-mapping exception="java.lang.Exception" result="exception"></exception-mapping>
    <exception-mapping exception="java.lang.Error" result="error"></exception-mapping>
    <exception-mapping exception="java.lang.RuntimeException" result="runtime_exception"></exception-mapping>
</global-exception-mappings>
 
    <action name="myaction" class="com.journaldev.struts2.exception.MyAction">
    </action>
    <action name="myspecialaction" class="com.journaldev.struts2.exception.MySpecialAction">
    <exception-mapping exception="java.io.IOException" result="login"></exception-mapping>
    <result name="login">/error.jsp</result>
    </action>
</package>​
Advertisement