Chapter 2. Web Layer Recipes

Table of Contents

1. Creating the Controller using Spring MVC
2. Creating the Controller using Spring Web Flow
3. Creating the Model for Spring MVC
4. Creating the Model for Spring Web Flow
5. Creating Model Packages
6. Creating Spring Components
7. Creating the View for Spring MVC
8. Scaffolding a Spring MVC application from domain object
9. Scaffolding Spring Security
10. Mapping URLs to Operations and Views
11. Creating Helper Methods using Operations
12. Reusing Operations in Different Contexts
13. Hiding the Implementation Technology
14. Implementing Post/Redirect/Get (PRG) Pattern
15. Restricting Direct Access to JSP Pages
16. Implementing Controller Methods using Actions and Steps
17. Blending Actions with non-generated Java Code
18. Blending Actions with non-generated Spring Beans
19. Implementing an HTML Form in JSP using standard HTML elements
20. Implementing an HTML Form in JSP using Spring Form Tag Library
21. Implementing an HTML Form in JSP using Skyway Tag Library
22. Using Third-Party Tag Libraries
23. Accessing Request and Session Parameters
24. Defining the Scope of Components, Controllers and Services
25. Managing User Session Data
26. Responding to User-Generated Events (Events and Commands)
27. Using Absolute Paths for Images, CSS and Javascript
28. Using CSS
29. Using Javascript
30. Securing Applications - Authentication
31. Securing Applications - Authorization
32. Validating End-User Input on Server using Spring Validator
33. Implementing Client-side Validation using Javascript
34. Implementing File Upload for Spring MVC
35. Implementing File Upload for Spring Web Flow
36. Streaming binary content

The web layer is also referred to as the UI layer. The web layer is primarily concerned with presenting the user interface and the behavior of the application (handling user interactions/events). While the web layer can also contain logic, core application logic is usually located in the services layer.

Figure 2.1. Model - View - Controller

Model - View - Controller

The Spring DSL artifacts for implementing the web layer of a Spring MVC application are the Controller, Operation, Component, Action, Steps and JSP pages. The Spring DSL artifacts for implementing the web layer of a Spring Web Flow application are the Flows and JSP pages. These artifacts will be described in detail in a following chapter.

1. Creating the Controller using Spring MVC


In the context of a Spring MVC application, the controller is responsible for receiving requests from a web client (typically from end-user generated events) and invoking a request handler called an Operation, which orchestrates all the server logic necessary for processing the request. Spring MVC supports the grouping of Operations related to an application task into a Controller. In order to gain the benefits of Spring MVC, a developer needs to have a pretty in-depth knowledge of Spring MVC. Creating a Spring Controller is tedious, and identifying the relationship between Spring Controllers, Model and Views is difficult because there's no easy way to identify them without digging through source code or code configuration files.


The Spring DSL lets you define your controllers using meta-data. To create a controller a developer must only define a controller and provide it a common name, and Skyway Builder will generate all the supporting code and Spring configurations needed. Every operation that is added to the controller will result in a handler method being generated into the Spring controller.


Controllers are used along with the Operations, Models and Views to implement an application using Spring MVC. The Web Controller has essentially two functions: group related Operations and map URLs to Operations, which orchestrate the task and support the user events associated with the task.

Figure 2.2. Controller (MVC)

Controller (MVC)

Steps for creating a Controller:

  1. Right click the on a Model Package, and select New-->Controller

  2. From the New Controller Wizard, enter a common name for the controller. The common name is for referencing the controller from other artifacts. Click Finish

The following figures shows an abbreviated version of the code that is generated from a Controller artifact. To see the fully generated code, see the PAM tutorial.

Example 2.1. Annotated Spring Controller (Generated)

package com.pam.web;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

// Imports for Spring framework classes, including annotation, stereotypes, binding, 
//         validation, and transation etc...
...full list omitted for brevity

// Imports statements for generated classes from referenced data types, services, and DAOs
...full list omitted for brevity

 * Request dispatcher for the <code>AppraisalEntryController</code> controller.
 * @generated
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, noRollbackForClassName = "java.lang.Exception", rollbackForClassName = "org.skyway.execution.exception.RollbackException")
public class BlogController {

  * Wire in the project level variables that can span both controllers and services.
  * @generated
  @Resource(name = "SkywayBlog")
  private SkywayBlog project;

  // Annotation-based code to wire in ModelAttributes

  // Controller Variables, including Getters and Setters

  // Initialize Bindings

  // Request Mapping Logic