1

I have a project that is using struts 2 TokenSessionStoreInterceptor. I assumed this interceptor would display an action error in the event it deemed the token invalid (cases where it isn't just a double submit but the token never existed). I noticed that the parent class TokenInterceptor has a resouce key you can override to provide internationalization or a custom error message. Is there a way to have the TokenSessionStoreInterceptor use this same resource key?

I ended up extending the TokenSessionStoreInterceptor with my own class and only overriding the handleInvalidToken() method, calling super and if super returned an invalid token result adding an action error. Such as what I have below:

public class CustomTokenSessionStoreInterceptor extends TokenSessionStoreInterceptor {

    @Override
    protected String handleInvalidToken(final ActionInvocation invocation) throws Exception {
        final String result = super.handleInvalidToken(invocation);

        if (INVALID_TOKEN_CODE.equals(result)) {
            final String errorMessage = this.getErrorMessage(invocation);
            Object action = invocation.getAction();

            if (action instanceof ValidationAware) {
                ((ValidationAware) action).addActionError(errorMessage);
            }
        }

        return result;
    }
}

I'm surpised that this isn't done by default, is there a built in way to provide this functionality?

Tom11
  • 2,419
  • 8
  • 30
  • 56
Dan King
  • 1,080
  • 1
  • 11
  • 28
  • 2
    Why do you use `tokenSession` interceptor instead of `token` if you want to display some error message? See [Difference between Token Interceptor and Token Session Interceptor?](http://stackoverflow.com/a/19526756/1700321). – Aleksandr M Mar 04 '16 at 09:26
  • Because our end goal is to handle multiple form submissions gracefully and the token session interceptor provides that for us. It just doesn't allow for any sort of error message when the token doesn't exist at all – Dan King Mar 04 '16 at 13:09
  • With the session store interceptor an invalid or missing token returns an invalid token result, which we want with an error message. If its a double form submission then we want it to return the result form the first form submission and not process the second form submission. The token session interceptor allows for both of these cases but doesn't allow an error message in the event of a missing or invalid token. That is why I extended it. I'm just curious if struts provides something out of the box that gives me what I have implemented – Dan King Mar 04 '16 at 13:23
  • Of if there is an explanation why no action error message is added in the token session interceptor when the token is invalid – Dan King Mar 04 '16 at 13:24
  • 1
    Ok. Now it is clear. No, seems there is no a built in way. Extending is a way to go. `TokenSessionStoreInterceptor` can be improved to add error message just as `TokenInterceptor` do. You can submit an issue [here](https://issues.apache.org/jira/browse/WW/) and provide a PR in the github - https://github.com/apache/struts. – Aleksandr M Mar 04 '16 at 13:57

0 Answers0