You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

The example code for this tutorial, Exception_Handling_Struts2_Ant or Exception_Handling_Struts2_Mvn, is available on Google Code - http://code.google.com/p/struts2-examples/downloads/list. After downloading and unzipping the file, you'll have a folder named Exception_Handling_Struts2_Ant (or Exception_Handling_Struts2_Mvn). In that folder will be a README.txt file with instructions on now to build and run the example application.

Introduction

In this tutorial we'll explore using 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.

The code discussed in this tutorial can be download from Google Code - http://code.google.com/p/struts2-examples/downloads/list.

The Struts 2 user mailing list is an excellent place to get help. If you are having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don't find an answer to your problem, post a question on the mailing list.

Global Exception Handling

Using the Struts 2 framework you can specify in the struts.xml how the framework should handle uncaught exceptions. The handling logic can apply to all actions (global exception handling) or to a specific action. Let's first discuss how to enable global exception handling.

To enable global exception handling you need to add two nodes to struts.xml: global-exception-mapping and global-results. For example examine struts.xml from the Exception Handling code download (http://code.google.com/p/struts2-examples/downloads/list).

  
   <global-exception-mappings>
	<exception-mapping exception="org.apache.struts.register.exceptions.SecurityBreachException" result="securityerror" />
	 <exception-mapping exception="java.lang.Exception" result="error" />
   </global-exception-mappings>
 
  <global-results>
        <result name="securityerror">/securityerror.jsp</result>
  	<result name="error">/error.jsp</result>
   </global-results>
 

The global exception mapping node tells the Struts 2 framework what to do if an uncaught exception of the type specified (or a child of that type) is thrown by application. For example if a SecurityBreachException is thrown but not caught, the Struts 2 Action class will return a result of "securityerror". All other uncaught exceptions will cause the Struts 2 Action class to return a result of "error".

The global results mapping node relates the result value to a specific view page. For example the result "securityerror" will cause the framework to redirect the user's browser to the securityerror.jsp view page.

Exception Handling Per Action

If you need to handle an exception in a specific way for a certain action you can use the exception-mapping node within the action node.

  • No labels