struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Greg Lindholm <>
Subject [S2] Preserving messages across a Redirect in Struts 2
Date Wed, 02 Jul 2008 19:29:50 GMT
Hi All,

I took a look at using MessageStoreInterceptor  to preserve messages across a redirect and
didn't like what I saw, it's too hard to configure.  So I wrote an Interceptor RedirectMessageInterceptor
(included below) that will automatically preserve messages across a redirect or redirectAction.

The way it works is… it looks at the result type after
a action has executed and if the result was a redirect
(ServletRedirectResult) or a redirectAction
(ServletActionRedirectResult) and there were any errors, messages, or
fieldErrors they are stored in the session.  Before the next action
executes it will check if there are any messages stored in the session
and add them to the next action.

The one thing you need to be aware of is: The action you are
redirecting towards will need to configure a result with name=”input”
as the added messages will trigger the ‘workflow’ interceptor to return
a result of “input”.
I've also posted this here:

Feel free to use or modify this as you see fit:

package my.struts.interceptor;
import java.util.Collection;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.dispatcher.ServletActionRedirectResult;
import org.apache.struts2.dispatcher.ServletRedirectResult;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;
import com.opensymphony.xwork2.ValidationAware;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

public class RedirectMessageInterceptor extends MethodFilterInterceptor
    private static final long serialVersionUID  = 

    public static String fieldErrorsSessionKey = 
    public static String actionErrorsSessionKey = 
    public static String actionMessagesSessionKey = 

    public String doIntercept(ActionInvocation invocation) throws Exception
        Object action = invocation.getAction();
        if (action instanceof ValidationAware)
            before(invocation, (ValidationAware) action);

        String result = invocation.invoke();

        if (action instanceof ValidationAware)
            after(invocation, (ValidationAware) action);
        return result;

     * Retrieve the errors and messages from the session and add them 
     * to the action.
    protected void before(ActionInvocation invocation,
                          ValidationAware validationAware) throws
        Map session = invocation.getInvocationContext().getSession();

        Collection<String> actionErrors = 
            (Collection) session.get(actionErrorsSessionKey);
        Collection<String> actionMessages = 
            (Collection) session.get(actionMessagesSessionKey);
        Map<String, String> fieldErrors = 
            (Map) session.get(fieldErrorsSessionKey);


        if (actionErrors != null && actionErrors.size() > 0)
            for (String error : actionErrors)

        if (actionMessages != null && actionMessages.size() > 0)
            for (String message : actionMessages)

        if (fieldErrors != null && fieldErrors.size() > 0)
            for (Map.Entry<String, String> entry : fieldErrors.entrySet())


    * If the result is a redirect then store error and messages in the session.
    protected void after(ActionInvocation invocation,
                         ValidationAware validationAware) throws Exception
        Result result = invocation.getResult();

        if (result instanceof ServletRedirectResult
            || result instanceof ServletActionRedirectResult)
            ActionContext actionContext = 
            HttpServletRequest request = 
                (HttpServletRequest) actionContext.get(

            * If the session doesn't already exist then it's too late to 
           * create one as the response has already been committed.
           * This is really only to handle the 'unusual' case of a browser 
           * refresh after the session has expired. In this case the 
           * messages are lost :(
            HttpSession session = request.getSession(false);
            if (session != null)
                Collection actionErrors = 
                if (actionErrors != null && actionErrors.size() >

                Collection actionMessages = 
                if (actionMessages != null && actionMessages.size()
> 0)

                Map fieldErrors = validationAware.getFieldErrors();
                if (fieldErrors != null && fieldErrors.size() >

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message