PROBLEM
In the context of Spring MVC, a model generally represents the data that will be passed to and from an operation (defined in a web controller) and the view. Spring MVC supports various options and patterns for defining the data that can be passed back and forth, and a typical Spring MVC application may use a combination of these options. The flexibility of Spring MVC to have fine-grained control for defining models is very powerfull, however along with the flexibility comes considerable variability and complexity in the implemenation using Spring.
SOLUTION
Through the use of code generation in Skyway Builder, the complexity of Spring models can be abstracted to makes things simpler for the developer.
A controller method in Spring can support zero-to-many input parameters, and the principal mechanisms in Spring for specifying input parameters are the @RequestParam and the @ModelAttribute annotations. The @RequestParam annotation is used to bind individual request parameters, like string and integers, to method parameters in the controller. The @ModelAttribute annotation is used to bind complex objects, like domain objects, data transfer objects and/or form backing objects, to method parameters in a controller.
In Skyway Builder operations are used to define request handlers (controller methods), and operation parameters are simply configured using Input variables. Since these variables are available to be processed by the operation, the developer using Skyway Builder doesn't really need to be concerned with the details of Spring.
Nevertheless here's an overview of what gets generated for input variables added to an operation::
the input variable is emitted as a annotated method parameter
the annotaton is determined by the variable type
primitive variables will be annotated with @RequestParam
complex variables will be annotated with @ModelAttribute
the annotation name is derived from the variable name
A controller method in Spring can also output model data, and the principal mechanism in Spring for specifying output model data is the ModelAndView object. In the event that there isn't any data to be returned by the method, the controller method can simply return a String that represents the view that should be rendered. If the controller method does return data, then a ModalAndView object needs to be instantiated and each output variable is added as a model attribute to the ModelAndView object.
In Skyway Builder you simply need to define output parameters on the Operation, and Skyway Builder will handle all the details regarding how the model data should be emitted.
Here's an overview of what occurs regarding output variables added to an operation:
If you don't have any output variables, the method return type will be String, and the last line of the operation will return the corresponding view that should be rendered (as defined in URL mappings).
If you have one or more output variables, the method will return a ModelAndView object, and each output variable will added to the ModelView. The output variable name will be used as the ModelAttribute name, and the ModalAndView will be set with the view that should rendered (as defined in URL mappings).
HOW IT WORKS
Once again a model generally refers to the data that is pased to and from a controller method (Skyway Operation).
Controller methods parameters are defined using Operation Input and Variables.
Steps for defining operation input and output variables:
From the Skyway Navigator, double-click on the Operation to configure, and switch to the Inputs/Outputs tab.
Add input variables by clicking on the Add... button in the Inputs block, and specify the output variable name. Assign (assignment) the variable name to either an input variable or operation variable. The type and collection parameters will be automatically configured based on the selected assignment variable.
Let's examine a few scenarios to see exactly what gets generated based on the configuration of the operation inputs and outputs.
Example 1
In this first example, an operation was defined with two input variables and no output variables.
Example 2.2. Spring MVC Model (Example 1) - Operation Inputs/Outputs (Generated)
@RequestMapping(value = "/OrderCheckoutController/AddBillingAddress.action") public String addBillingAddress(@RequestParam("orderId") String orderId, @ModelAttribute("billingAddress") Address billingAddress) throws java.lang.Exception { //method implementation either hand-coded or generated from action model return "/billingoverview.jsp"; }
The operation didn't have any defined output variables. Therefore the return type for the controller method is String. | |
The orderId input variable is of type Text. Since Text is a primitive data type, the method parameter corresponding to this variable is annotated with the @RequestParam annotation. The variable name (orderID) is used as the name. | |
The billingAddress input variable is of type Address. Since Address is a complex type, the method parameter corresponding to this variable is annotated with the @ModelAttribute annotation. The variable name (billingAddress) is used as the name. | |
The output of the controller method is set to the path of the view that should be used for rendering the results. |
Example 2
In this second example, an operation was defined with one input variable and an output variable. The output variable is mapped to an operation variable (shippingOptions) that is defined in the Variables tab (not shown).
Example 2.3. Spring MVC Model (Example 2) - Operation Inputs/Outputs (Generated)
@RequestMapping(value = "/OrderCheckoutController/LoadShippingOptions.action") public ModelAndView loadShippingOptions(@RequestParam("zipcode") String zipcode) throws java.lang.Exception { ModelAndView mav = new ModelAndView(); Set<ShippingOption> shippingOptions = null; //method implementation either hand-coded or generated from action model mav.addObject("shipoptions", shippingOptions); mav.setViewName("/success.jsp"); return mav; }
The operation has atleast one output variable. Therefore the return type for the controller method is ModelAndView. | |
The zipcode input variable is of type Text. Since Text is a primitive data type, the method parameter corresponding to this variable is annotated with the @RequestParam annotation. The variable name (zipcode) is used as the name. | |
Since the return type of the controller method is a ModelAndView object, a ModelAndView object is instantiated. | |
The operation output variables are added to the ModelAndView object. The variable name is used as the model attribute name, and the object (shippingOptions) that was assigned to the output is used as the model. | |
The view is set on the ModelAndView object to the path of the view that should be used for rendering the results. |
RELATED RECIPES