Example: Simple Calculator

This example demonstrates how you can resolve a users request directly to a method on any POJO. In Waffle terminology we call this an ActionMethod.

NOTE:
Validation and error handling have been ignored for this example

Controller: CalculatorController

As you can see the Controller is an extremely simple object. The three methods add, subtract, and multiply are themselves ActionMethods. Notice that they each have 2 arguments. Additionally each method has a unique type for its arguments (int, long, float). Waffle will automatically convert the users request into the correct type. This conversion is NOT limited to primitives, custom conversion is easily supported but out of scope for this example (see Binding for further details).

Registrar

Registration is as you would expect. In this example the Controller must be registered to the session level so that users are not sharing the same result property.

View: calculator.jspx

The jspx code below is not very exciting however notice lines 43 - 45. These 3 lines demonstrate different ways to invoke an ActionMethod. Line 43 add|firstNumber|secondNumber pragmatically defines how the method arguments should be resolved. See the ActionMethod documentation for complete details.

Lines 44 and 45 in the example above do not pragmatically define how their argument values are to be resolved. So in order to handle this we can add an annotation to each of those methods in the Controller. Below we have updated the CalculatorController with two ActionMethod annotations (lines 12 and 17).

Annotated Controller: CalculatorController