Thread:
 Exception handling with Struts 
 damiel   14 Feb 2007, 04:59 

Comment
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
From: damiel (14 Feb 2007, 04:59) Replies: 0, Views: 15944
Subject: Exception handling with Struts
First of all, my last comment was about HibernateSessionRequestFilter,
not about HibernateSessionConversationFilter as I wrote (I don't know if
it also applies to the latter) - sorry for that.

It was also hard for me to find out what to do in place of the "Let
others handle it..." comment in the HibernateSessionRequestFilter. We
are using Hibernate in a Struts application and of course would like to
present error messages on the next page to the user if the commit fails.

I had some success with the following method to create Struts error
messages:

/**
 * Append an error message to the next user visible page.
 * @param request HttpServletRequest
 * @param key Message key for this message 
 * @param value0 First replacement value
 */
private void addErrorMessage(ServletRequest request, String key, Object
value0)
{
    if (request instanceof HttpServletRequest)
    {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpSession session = httpRequest.getSession();

        ActionErrors actionErrors = (ActionErrors)
session.getAttribute(Globals.ERROR_KEY);
        if (actionErrors == null)
        {
            actionErrors = new ActionErrors();
            session.setAttribute(Globals.ERROR_KEY, actionErrors);
        }

        ActionMessage newMessage = new ActionMessage(key, value0);
        actionErrors.add(ActionErrors.GLOBAL_MESSAGE, newMessage);
    }
    else
    {
        logger.error("servlet filter request is no HttpServletRequest");
    }
}

In the exception handler, I check for several cases to create suitable
messages like this (shortended, there are more cases in reality):

if (ex instanceof JDBCException)
{
    logger.error("JDBCException", ex);
    SQLException sqlException = ((JDBCException)ex).getSQLException(); 
    addErrorMessage(request, "global.error.jdbcException",
sqlException.toString());
}
else
{
    logger.error("Unknown session error", ex);
    addErrorMessage(request, "global.error.unknown", ex.toString());
}

It's questionable if the Struts developers expected anybody to do this
from inside a servlet filter, but currently I have no better idea how to
pass this information to the presentation layer.
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
© Copyright 2006, Red Hat Middleware, LLC. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc. [Privacy Policy]