In this tutorial we'll explore how to enable the Struts 2 framework to handle any uncaught exceptions generated by a web application. Struts 2 provides robust exception handling, including the ability to automatically log any uncaught exceptions and redirect the user to a error web page.
Let's handle an exception for a certain action for that you can use the exception-mapping node within the action node.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="com" extends="struts-default">
<action name="login" class="com.candidjava.controller.Login">
<exception-mapping result="error"
exception="java.lang.ArithmeticException"></exception-mapping>
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
Screenshot
Download
Struts 2 exception handling war
Struts 2 exception handling maven war