Mentawai Web Framework

Inner Actions

An action can have other sub-actions or inner actions in its implementation. The main funcionality of an action is defined by its execute() method, but you can create other methods to perform different functionalities inside the same action class.

Below is an example of an action implementation that has a inner action:

public class HelloInnerActions extends BaseAction {
	
	public String execute() throws Exception {
		output.setValue("speak", "Hello there!");
		return SUCCESS;
	}
	
	public String sayBye() throws Exception {
		output.setValue("speak", "Bye for now...");
		return SUCCESS;
	}
	
}
			

To invoke an inner action, you should append its name just before the Mentawai extension in the action URL. Examples:

http://www.yourapp.com/HelloInnerActions.sayBye.mtw
http://www.yourapp.com/yourcontext/categories.add.mtw?id=10

An inner action can have its own consequences and filters, and can be used independently from the regular action. Check the examples below:

public class ApplicationManager extends org.mentawai.core.ApplicationManager {
	
    public void loadActions() {
        
        // Example 1)
        
        // Define an action config for a regular action (execute() method)
        // and you can call any inner action that it will use the configuration
        // of the regular action
        
        ActionConfig ac = new ActionConfig("/HelloInnerAction", HelloInnerActions.class);
        ac.addConsequence(HelloMentawai.SUCCESS, new Forward("/hello.jsp"));
        addActionConfig(ac);

        // Example 2)
        // You can define an inner action consequence, 
        // to be used instead of the regular action consequence...
        
        ac = new ActionConfig("/HelloInnerAction2", HelloInnerActions.class);
        ac.addConsequence(HelloMentawai.SUCCESS, new Forward("/hello2.jsp"));
        ac.addConsequence(HelloMentawai.SUCCESS, "sayBye", new Forward("/bye2.jsp"));
        addActionConfig(ac);        
        
        // Example 3)
        // You can define an inner action config, 
        // so that consequences and filters can be defined solely for this inner action...
        
        ac = new ActionConfig("/HelloInnerAction3", HelloInnerActions.class, "sayBye");
        ac.addConsequence(HelloMentawai.SUCCESS, new Forward("/hello3.jsp"));
        
        // this line below will thrown an exception
        // it doesn't make sense to define an inner action consequence of an inner action config...
        ac.addConsequence(HelloMentawai.SUCCESS, "sayBye", new Forward("/bye3.jsp")); // exception...
        
        // adding a filter, which will apply only for this inner action
        ac.addFilter(new SayByeValidationFilter());
        addActionConfig(ac);                
    }
}
	

In example 1) we are defining a regular action config with a regular consequence. Notice however that the HelloInnerActions class may implement a "sayBye" inner action and nothing holds you from calling it with the URL /HelloInnerActions.sayBye.mtw. Since you did not specify any inner action consequence, the "sayBye" inner action will use the regular consequences.

In example 2) we are defining a regular action config, but this time we are defining an inner action consequence. An inner action first checks for an inner action consequence. If it does not find one, it checks for a regular action consequence.

In example 3) we are defining an inner action config, so all consequences and filters for this action config will be specific to the inner action. There is no regular action here, so calling /HelloInnerAction3.mtw will throw a no action found exception. Also note that for example 3) it does not make sense to define an inner action consequence, since all consequences are for the inner action specified in the action config.

If you prefer, you may also define new results to differentiate between regular and inner actions. Check the example below:

public class ApplicationManager extends org.mentawai.core.ApplicationManager {
	
    public void loadActions() {

        ActionConfig ac = new ActionConfig("/HelloInnerAction", HelloInnerActions.class);
        // this can be returned by the execute() method...
        ac.addConsequence(HelloMentawai.HELLO, new Forward("/hello.jsp"));
        // this can be returned by the sayBye() method...
        ac.addConsequence(HelloMentawai.BYE, new Redirect("/bye.jsp"));
        addActionConfig(ac);
        
    }
}
	
You may click here to download an example application that illustrates the use of inner actions.