Download this <global-exception> example – Struts-Global-Exception-Example.zip

In Struts framework, the <global-exception> is used to display your custom error page, instead of the default classic HTTP Status 500 error page :

struts-global-exception-1

The default error page look ugly and not professional at all. In addition, the error message is descriptive enough to leak your system information to end user.

1. <global-exception> + default exception handler

Here’s a <global-exception> with default exception handler example declared in “struts.config.xml” to replace the default HTTP 500 error page with your custom descriptive error page.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
 
<struts-config>
 
	<form-beans>
		//...
	</form-beans>
 
        <global-exceptions>
	    <exception
	      key="error.global.mesage"
	      type="java.io.IOException"
	      path="/pages/error.jsp" />
	</global-exceptions>
 
	<action-mappings>
		//...
	</action-mappings>
 
</struts-config>

In above, the default Struts exception handler “org.apache.struts.action.ExceptionHandler” will be called when any IOException is thrown by an Action, and forward it to error.jsp file. The key is a key in your message resources properties file.

Common.properties

#common module error message
error.global.mesage = 
   Ooooppps... Sometime wraong in this site, please come back later

error.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
 
<html>
<head>
</head>
<body>
<h1>Struts Global Exception Example</h1>
 
<html:errors/>
 
</body>
</html>

2. <global-exception> + custom exception handler

In the default exception handler, you have no way to control how to deal with the exception. In most cases, you may need to log the exception for further analysis. To do this, you need a custom exception handler to log all the exceptions to another data store like file system or database.

Here’s a custom exception handler example to configure for “java.lang.Exception” so that it’s called for any exception thrown by Action. To create a custom exception handler, you need to subclass “org.apache.struts.action.ExceptionHandler” and override the execute method.

MyCustomExceptionHandler.java

package com.mkyong.common.exception;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ExceptionHandler;
import org.apache.struts.config.ExceptionConfig;
 
public class MyCustomExceptionHandler extends ExceptionHandler{
 
  private static final Logger logger = 
      Logger.getLogger(MyCustomExceptionHandler.class);
 
  @Override
  public ActionForward execute(Exception ex, ExceptionConfig ae,
	ActionMapping mapping, ActionForm formInstance,
	HttpServletRequest request, HttpServletResponse response)
	throws ServletException {
 
	//log the error message
	logger.error(ex);
 
	return super.execute(ex, ae, mapping, formInstance, request, response);
  }
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
 
<struts-config>
 
	<form-beans>
		//...
	</form-beans>
 
        <global-exceptions>
	    <exception
	      key="error.global.mesage"
	      type="java.io.IOException"
	      handler="com.mkyong.common.exception.MyCustomExceptionHandler"
	      path="/pages/error.jsp" />
	</global-exceptions>
 
	<action-mappings>
		//...
	</action-mappings>
 
</struts-config>

In above, when exception is thrown by Action, it will call your custom exception handler MyCustomExceptionHandler’s execute() method instead of the default exception handler.

Reference

  1. Struts Exception handler – http://struts.apache.org/1.x/userGuide/building_controller.html
Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~
[ Read More ] You can find more similar articles at Struts 1.x Tutorials