Is there a way to configure whether a struts action can be invoked?
For example, I have the following class:
public class MyAction {
public String myMethod() {
// logic
}
}
and the struts.xml file:
<action class="MyAction" method="myMethod"/>
Can I add in this file a configuration that let's me disable the invocation of this action like for example:
<action class="MyAction" method="myMethod">
<param name="disable">true</param>
</action>
A use case of this may be when I want to disable the execution of an action in dev mode, i.e I have an action that I invoke it from the client using AJAX. The invocation of the action ensures an important feature of my app. This feature is mandatory for the app to work properly. However, this feature may be a burden in dev mode, thus disabling it (only in this mode) will be so useful.
One approach to resolve this is by using the interceptor mechanism (as suggested in the comments). However, can this be done at the config level?
There are multiple ways how can you achieve that. I will list just a few of them.
Create and add into your dev configuration a custom interceptor which will check current action name against some blacklist and will skip action execution if it in this list. (You can put action names blacklist into some properties file.)
Change myMethod in method attribute in struts.xml file to some method in your action which simple returns NONE result. In that way you can easily replace it when building for production, by hand or preferable with your favorite build tool.
You can also create separate struts.xml file for your dev environment and swap it for production build.
Assuming that in your dev environment struts.devMode constant is set to true, you can inject it in your action class and use it in the method to check whether 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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With