02 May 2008

Struts Custom Error Message ,Custom ERROR.JSP ,Global Exception Forwards

Basically this article guides to make a custom.jsp for all the errors that might occur in your struts enables application.
Please follow the instructions for the same.
** Same Color Variables indicate references.

In the Base action … make this method…

/*
 * To display any common error with default message
 */

public ActionForward displayFatalError(ActionMapping mapping, Exception e,
                                    HttpServletRequest request) {
                        ActionErrors errors = new ActionErrors();
                        ActionError error = new ActionError(err.fatal, e.getMessage());
                        errors.add(ActionErrors.GLOBAL_ERROR, error);
                        // If a message is required, save the specified key(s) into the request
                        // for use by the <html:errors/> tag.
                        if (!errors.isEmpty()) {
                                    saveErrors(request, errors);
                        }
                        return mapping.findForward("exception");
}
 

While catching an exception in the Action Class that extends BaseAction… just do this…

catch (Exception e)
{
return displayFatalError(actionmapping, e, request);
}

 

Now this opens the error_login.jsp incase of any exception with the error message on it if <html: errors/> is included in the JSP.

For this … Follow below.

 

All this to be done is the Struts-Config.XML
First make the ApplicationResources.properties file in WEB-INF/classes folder like this
Basically it looks like this

 

###############################################################
# Generic labels
###############################################################

errors.prefix=<li>
errors.suffix=</li>

###############################################################
# Login messages
###############################################################
err.fatal=Please contact system administrator {0}

 

*Do not miss this {0} in the end as this will print the exception on the page concatenated with
The error message from the ApplicationResources.properties file.
(It's the first elements of the error stack trace… you can print more as {1}, {2}…)
then declare it in the struts-config like this :

<message-resources parameter="ApplicationResources"/>

Then this on the top of the struts-config.xml just after the <form-bean> and the

<data-sources> tags.

<global-forwards type="org.apache.struts.action.ActionForward">
            <forward name="exception" path="exception" />          
..more forwards
            </forward>
</global-forwards>

 

Then this is the tiles-def.xml

<definition name="exception" path="/GSA/error_login.jsp">        
</definition>

 
(if incase you have used tiles.. else define the redirection JSP
in the struts-config itself by giving its path rather than the link to tiles-def tag name)
as

<global-forwards type="org.apache.struts.action.ActionForward">
            <forward name="exception" path="/GSA/error_login.jsp" />          
..more forwards
            </forward>
</global-forwards>

 
If you want specific forwards then you can define the same in the
Web.xml also like this


<error-page>
   <error_code>500</error-code>
   <location>/error.jsp</location>
</error-page>

 
<error-page>
 <error-code>404</error-code>
 <location>/pages/404.jsp</location>
</error-page>