Why do I like working with the Apache Struts 2? Because I can simple change the framework behaviour and customise it to what I needed. And not just because you have plenty of options to modify, but especially because you can simple develop your own class which can replace version provided by the framework. Yeah, you can replace almost every framework class – Struts 2 was designed with that on mind 🙂

Recently I got involved in working on some old issue – WW-1967 Dynamic Method Invocation, validator with ActionName-aliasName-validation.xml files. The problem is related to Dynamic Method Invocation functionality and how the name of validation definition XML file is created. By default the file name will base on an action class and defined alias name and suffixed with -validation.xml, eg:

<action name="Login" class="org.demo.example.LoginAction">
    <result>/example/Login.jsp</result>
</action>

for action defined as above, the validation definition file name will be constructed as follow:

LoginAction-Login-validation.xml

There be a problem when you want to use DMI to call any public action’s method – which isn’t the best practise, though 😉 Anyway for each method the same validation file will be used, which ins’t a good idea. So, if you try to call an action like this – Login!check.action, the above file will be used to validate input as method name isn’t included in logic used to resolve file name.

How it can be solved? Quite simply, validation is provided by ValidationInterceptor which delegates execution to ActionValidatorManager to perform the validation process. But the context name is constructed by the mentioned interceptor in method:

public String getValidationContext(ActionProxy proxy)

So, to solve the problem you can extend ValidationInterceptor and override the method to provide your own context name like below:

@Override
protected String getValidationContext(ActionProxy proxy) {
    if (dmiEnabled && StringUtils.isNotEmpty(proxy.getMethod())) {
         return proxy.getActionName() + "_" + proxy.getMethod();
    }
    return super.getValidationContext(proxy);
}

After that, the file name will be created as follow:

LoginAction-Login_check-validation.xml

I’ve prepared a small demo how to do it – just check it out from GitHub.

And at the end see this talk from TED and give 10 minutes to solve all your problems 🙂