3

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?

ahmehri
  • 756
  • 2
  • 8
  • 26
  • 1
    What is the use case for that? – Aleksandr M May 21 '15 at 09:33
  • What result you want to be returned from your AJAX call to a disabled action ? – Andrea Ligios May 21 '15 at 09:58
  • 3
    Add some interceptor in your dev configuration and skip execution of needed actions there. – Aleksandr M May 21 '15 at 10:01
  • 1
    @AndreaLigios it doesn't matter the returned result when the action is disabled. – ahmehri May 21 '15 at 10:17
  • 1
    Then create a custom interceptor, add it to your stack, and inside the interceptor check for the action name. If it is in your blacklist, stop the actoin invocation and return null – Andrea Ligios May 21 '15 at 10:21
  • instead of disable use can set redirect mode to home page. if you want to change your app behavior. –  May 21 '15 at 11:54
  • action name="MyAction" class="ActionClass"> Home –  May 21 '15 at 11:55
  • @YogendraSharma the action is invoked by AJAX and it doesn't affect the views. – ahmehri May 22 '15 at 10:44
  • @ahmehri: How about changing method attribute to some default method which does nothing? – Aleksandr M May 26 '15 at 10:27
  • @AleksandrM that involves modifying `struts.xml` file, However using a config allows the switch of enabling/disabling the action invocation without modifying the code. – ahmehri May 26 '15 at 13:42
  • Which config you are referring to? You are going to modify `struts.xml` anyway, there is no need to modify the code. – Aleksandr M May 26 '15 at 15:45
  • by modifying the code, I meant modifying the `struts.xml` file, the best solution until now is to use an interceptor that reads a config and determines whether to invoke the action or not. – ahmehri May 26 '15 at 16:14

1 Answers1

1

There are multiple ways how can you achieve that. I will list just a few of them.

  1. 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.)

  2. 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.

  3. You can also create separate struts.xml file for your dev environment and swap it for production build.

  4. 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;
    }
    
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143