Disable calling the Struts action
Is there a way to configure if the struts action can be called?
For example, I have the following class:
public class MyAction {
public String myMethod() {
// logic
}
}
and struts.xml
:
<action class="MyAction" method="myMethod"/>
Can I add a configuration to this file that would allow me to disable the call to this action, for example:
<action class="MyAction" method="myMethod">
<param name="disable">true</param>
</action>
This might be the case where I want to disable the execution of an action in dev mode, i.e. I have an action that I am calling from the client using AJAX. Calling an action provides an important function of my application. This feature is required for the application to function properly. However, this feature can be a burden in dev mode, so it can be disabled (only in this mode).
One approach to solving this issue is to use an interceptor mechanism (as suggested in the comments). However, can this be done at the configuration level?
source to share
There are several ways you can achieve this. I will list just a few of them.
-
Create and add to your developer config a custom interceptor that will check the current action name for some kind of blacklist and will skip the action if it is in this list. (You can blacklist action names in some properties file.)
-
Change
myMethod
in the attributemethod
in the filestruts.xml
to some method in your action that just returns the resultNONE
. This way, you can easily replace it when crafting for handcrafted production, or preferably with your favorite build tool. -
You can also create a separate file
struts.xml
for your development environment and change it to build the assembly. -
Assuming a
struts.devMode
constant has been set in your development environmenttrue
, you can inject it into your action class and use it in a method to check if it should be executed or not.private boolean devMode; @Inject(StrutsConstants.STRUTS_DEVMODE) public void setDevMode(String mode) { devMode = Boolean.valueOf(mode); } public String myMethod() { if(devMode) { return NONE; } // ... return SUCCESS; }
source to share