Table of Contents
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.
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.
PROBLEM
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.
SOLUTION
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.
HOW IT WORKS
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.
Steps for creating a Controller:
Right click the on a Model Package, and select New-->Controller
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.multipart.support.ByteArrayMultipartFileEditor; 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 */ @Scope("singleton") @Controller("AppraisalEntryController") @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 ... }
RELATED RECIPES