3. Creating the Model for Spring MVC

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::

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:

HOW IT WORKS

Once again a model generally refers to the data that is pased to and from a controller method (Skyway Operation).

Figure 2.5. Model (MVC)

Model (MVC)

Controller methods parameters are defined using Operation Input and Variables.

Steps for defining operation input and output variables:

  1. From the Skyway Navigator, double-click on the Operation to configure, and switch to the Inputs/Outputs tab.

  2. 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.

Figure 2.6. Spring MVC Model (Example 1) - Operation Inputs/Outputs

Spring MVC Model (Example 1) - Operation Inputs/Outputs

The following figures shows an abbreviated version of the full controlller method that is generated from the operation configuration.

Example 2.2. Spring MVC Model (Example 1) - Operation Inputs/Outputs (Generated)

@RequestMapping(value = "/OrderCheckoutController/AddBillingAddress.action")
public String1 addBillingAddress(@RequestParam("orderId") String orderId2, 
                                @ModelAttribute("billingAddress") Address billingAddress3) throws java.lang.Exception {

  //method implementation either hand-coded or generated from action model

  return "/billingoverview.jsp";4
}

1

The operation didn't have any defined output variables. Therefore the return type for the controller method is String.

2

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.

3

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.

4

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).

Figure 2.7. Spring MVC Model (Example 2) - Operation Inputs/Outputs

Spring MVC Model (Example 2) - Operation Inputs/Outputs

The following figures shows an abbreviated version of the full controlller method that is generated from the operation configuration.

Example 2.3. Spring MVC Model (Example 2) - Operation Inputs/Outputs (Generated)

@RequestMapping(value = "/OrderCheckoutController/LoadShippingOptions.action")
public ModelAndView1 loadShippingOptions(@RequestParam("zipcode") String zipcode2) throws java.lang.Exception {
  ModelAndView mav = new ModelAndView();3
  Set<ShippingOption> shippingOptions = null;

  //method implementation either hand-coded or generated from action model

  mav.addObject("shipoptions", shippingOptions);4
  mav.setViewName("/success.jsp");5
  return mav;
}

1

The operation has atleast one output variable. Therefore the return type for the controller method is ModelAndView.

2

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.

3

Since the return type of the controller method is a ModelAndView object, a ModelAndView object is instantiated.

4

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.

5

The view is set on the ModelAndView object to the path of the view that should be used for rendering the results.

RELATED RECIPES

  1. Creating a Controller for Spring MVC

  2. Creating the View for Spring MVC

copyright@2023. skywayperspectives. All rights reserved