AOP MethodInterceptor breaks struts2 Action/page

2019-08-30 16:37发布

问题:

I am having a wierd issue with struts2 and aop. I need to intercept certain struts2 actions for checking some custom settings. The actions are getting intercepted but it breaks all page parameters/form values etc:

The AOP configuration is:

<aop:advisor id="associateModuleCheck"  advice-ref="associateModuleAdvice"  pointcut="execution(* uk.co.company.package.webapp.action.*.ModuleA*.*(..))" order="1"/>

And the method Interceptor

public Object invoke(MethodInvocation invocation) throws Throwable {
        Class<?> targetClass = invocation.getThis().getClass();
        // DO stuff 
        return invocation.proceed();
}

Basically I can see that the action methods are intercepted, but when it's returned to the page, it breaks all form values. (Some actions won't even reach the MethodInterceptor method but I can see from the Spring log that it's basically intercepted) :

Candidate is: '/namespacebbb/ModuleAction/method.action'; pattern is /**; matched=true

The reason why I am not using Struts2 interceptor is because I can configure a whole lot of actions in one go using Spring AOP.

If I remove the interceptor everything works fine. Anybody done this stuff before? Any idea what's wrong?

Update:

Though I finally ended up using a struts2 interceptor, this issue is still open. I found this spring forum question, which is very similar to my issue:

http://forum.spring.io/forum/spring-projects/aop/38625-aop-struts-2-help

回答1:

After a bunch of digging, I think I found the issue (if you're using Spring to help with AOP, even if you're not you're probably going to need a different ObjectFactory), long and short of it is that you need to make sure that the struts ObjectFactory is setup properly:

<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
<constant name="struts.objectFactory.spring.autoWire.alwaysRespect" value="true"/>

or

<constant name="struts.objectFactory" value="spring" />
<constant name="struts.objectFactory.spring.autoWire.alwaysRespect" value="true"/>

source: http://www.javawebdevelop.com/3294124/