Mentawai allows you to use any Pojo (Plain Old Java Object) as an action, so you do not need to couple your actions with the framework if you don't want to.
Writing your Pojo Action:
public class HelloPojo { private String msg; public void sayHello() { msg = "hello from sayHello method!"; // assume result is SUCCESS when method returns void } public String sayHi() { msg = "hi from sayHiMethod!"; return "success"; // you can return anything here and the toString() method will be used to create a result // if you return null, then the result will be assumed to be "null" } public void addTwoNumbers(int number1, int number2) { msg = "Result1: " + (number1 + number2) + " / number1=" + number1 + " / number2=" + number2; // void method so SUCCESS assumed as the result } // A property that will be made available in the action output public String getMsg() { return msg; } }
NOTE: All properties from the action (getters) will be placed automatically in the action output. So for the action above you should expect the value of the message to be in the action output with the key "msg".
And nothing changes when you configure the action in the application manager:
action("/HelloPojo", HelloPojo.class) // Note: Since we did not specify any method, all methods will use this configuration .on(SUCCESS, fwd("/jsp/hello.jsp"));
How the controller chooses an action method:
The Mentawai controller sorts the action parameters alphabetically by name and with that order it tries to find an action method that can accept the parameters. For example:
http://www.myapp.com/HelloPojo.addTwoNumbers.mtw?b=2&a=3
will call:
addTwoNumbers(3, 2); // a=3, b=2 even though in the query string the parameter 'b' comes before 'a'
If you want to order yourself the parameters, you can use the pojoParams method in the application manager, for example:
action("/HelloPojo", HelloPojo.class) // Note: Since we did not specify any method, all methods will use this configuration .pojoParams("b", "a") .on(SUCCESS, fwd("/jsp/hello.jsp"));
will call:
addTwoNumbers(2, 3); // bypass the alphabetical order by using the pojoParams method
TIP: You can also use the pojoParams method when you have too many parameters and you want to make sure the correct ones are picked up by the action method.