Tuesday, December 4, 2007

Struts Redirect does not forward errors

The struts framework stores the Action Errors in the request object and hence when we redirect an action in the struts-config.xml, the errors saved may disappear.
<forward name="close" redirect="true" path="/logoutAction.do" contextrelative="false" >

Solution

Put the errors in session scope as <html:errors/> reads ActionErrors from session context too

Example

private ActionErrors errors = new ActionErrors();
try
{
insertNewEmployee();
}
catch (SQLException e)
{
errors.add(ActionErrors.GLOBAL_MESSAGE,
new ActionMessage("errors.database.unableToInsert"));
}
saveErrors(request.getSession(), errors);

No comments: