Com o Mentawai você pode utilizar qualquer Pojo (Plain Old Java Object) como sua action. Dessa maneira você pode escrever sua action com 0% (isso mesmo, 0%, nem mesmo annotations) de acoplamento com o framework.
Escrevendo sua action como um Pojo:
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; } }
NOTA: Todas as propriedades da sua Pojo Action serão colocadas automaticamente no output da action. Logo para a pojo action acima o método getMsg() será chamado e colocado no output da action com a chave "msg".
Nada muda para configurar a action no 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"));
Como o controlador escolhe o método da action:
O controlador do Mentawai ordena os parâmetros da requisição por ordem alfabética e tenta encontrar um método que pode receber esses parâmetros. Por exemplo:
http://www.myapp.com/HelloPojo.addTwoNumbers.mtw?b=2&a=3
vai chamar:
addTwoNumbers(3, 2); // a=3, b=2 even though in the query string the parameter 'b' comes before 'a'
Se você quizer escolher a ordem dos parâmetros, você pode usar o método methodParams() no application manager. Por exemplo:
action("/HelloPojo", HelloPojo.class) // Note: Since we did not specify any method, all methods will use this configuration .methodParams("b", "a") .on(SUCCESS, fwd("/jsp/hello.jsp"));
vai chamar:
addTwoNumbers(2, 3); // bypass the alphabetical order by using the methodParams method
DICA: Você também pode usar o methodParam() quando há muitos parâmetros na requisição e você quer escolher quais serão utilizados pela action.