1

How to create custom exception handler class in Struts2? I have tried adding the exception code in execute() but failed.

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
swetha
  • 449
  • 7
  • 20

1 Answers1

2

I hope that this guidelines will be usefull for your question, since it looks a little too broad, so if you have a specific requirement just edit your question. First of all I suggest you to read the official documentation in order to retrieve information about the Exception handling, Exception configuration and Exception Interceptor.

Now, I try to be more detailed as I can two explain how you can handle the exceptions in Struts2:

  • Global exception handling: everytime your application throws an exception, Struts2 will handle the result with the <global-results> and <global-exception-mappings> tags in struts.xml file.
  • Action exception handling: everytime your specific action throws an exception, Struts2 will handle the result <exception-mapping> tag inside a <action> tag

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 project.

<global-results>
      <result name="securityerror">/securityerror.jsp</result>
      <result name="error">/error.jsp</result>
</global-results>

<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>

Now when a SecurityBreachException is thrown, the securityerror.jsp will be loaded. In my opinion global exception are very usefull for generic errors. For example you can create an ApplicationException view with a form used to send to your assistance the stack trace of the problem.

Action exception handling

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.

<action name="actionspecificexception" class="org.apache.struts.register.action.Register" method="throwSecurityException">
      <exception-mapping exception="org.apache.struts.register.exceptions.SecurityBreachException" result="login" />
      <result>/register.jsp</result>
      <result name="login">/login.jsp</result>
</action>

In this example when throwSecurityException method of the Register action throws a SecurityBreachException it returns a LOGIN result and the login.jsp is loaded. I've used this kind of solution for actions that may throw errors after a validation (for example to execute CRUD operations in my database).

Custom error jsp

By default, the ExceptionMappingInterceptor adds the following values to the Value Stack:

  • exception : The exception object itself
  • exceptionStack : The value from the stack trace

You can use them to create a custom jsp that prints the error occurred and the stacktrace. Here you have an example:

<h2>An unexpected error has occurred</h2>
<p>
    Please report this error to your system administrator
    or appropriate technical support personnel.
    Thank you for your cooperation.
</p>
<hr/>
<h3>Error Message</h3>
<s:actionerror/>
<p>
    <s:property value="%{exception.message}"/>
</p>
<hr/>
<h3>Technical Details</h3>
<p>
    <s:property value="%{exceptionStack}"/>
</p>

Logs

I think that also the LoggingExceptions paragraph will be usefull.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
IlGala
  • 3,331
  • 4
  • 35
  • 49