10. Mapping URLs to Operations and Views

PROBLEM

In the context of Spring MVC there are request handlers, called Operations in the Spring DSL, that are implemented in the controller for defining the behavior of the application and handling user-generated events. There needs to be a strategy for a Controller to identify (a) which Operations are available to web clients, (b) what URL will be used to address the Operation, and (c) which View should be used to render the response.

SOLUTION

In addition to serving as the container for Operations, a Controller defines URL Mappings, which associate URLs to Operations and Views. URL Mappings are the mechanism for specifying which Operations are going to be accessible to web clients. This is accomplished by defining one or more URL Mappings in the Controller.

HOW IT WORKS

A URL Mapping is responsible for defining the url(s) that will trigger an action. When the defined URL is requested from a web control, javascript or web client, the operation associated with (mapped to) the url will handle the request. The URL mapping will also specify the jsp that will render the response to the request. Every request will have a response, and the URL mapping specifies both the operation that will handle the request and the jsp that will render the response

Each mapping has several parameters:

Table 2.1. URL Mapping Parameters

ParameterDescription
URL

The URL being mapped, or intercepted by the MVC framework. The controller that the URL Mapping is being added to will handle all requests for the specified URL.

Request Handler

The operation that will handle the request or variable that data will be bound to.

When an Operation is specified as the request handler, the configured URL is being mapped to the functionality corresponding to the selected Operation.

When an Variable is specified as the request handler, the configured URL can be used for mapping data to the selected Variable. (This is primarily an AJAX scenario.)

While a request handler is typically specified, it's not required. If the request handler is omitted, then the URL will essentially function as an alias to the View.

View

The view that should be rendered following the invocation of the Operation; the configuration choices are:

  • A JSP page

  • View alias (defined at project level)

  • Controller or Operation variable

For a specific URL mapping the view cannot be the same as URL because you would end-up with a circular reference. While you can workaround this by using an alias to the URL, this approach is generally not recommended because it exposes the implementation technology to end-users and potential hackers. See Hide the Implementation recipse for the best practice approach.

Redirect: By default the request will be forwarded to the view on the server. The redirect option will instead send the browser a redirect command to the specified View.

Error View

The Error View functions very similarly to the View, except that it specifies the view that should be rendered following a binding or validation error. The configuration choices are the same as View.

Data Validation

The Spring validation class and method that should be used to validate the request.


The configurations needed to achieve the desired page flow pattern are represented in the following diagram:

Example 2.4. URL Mapping - Examples

URL                                     OPERATION                             VIEW 
----------------------------------      --------------------------------      ---------------------------------
/OrderController/LoadHistory.action --> LoadHistory                       --> history.jsp 1
/index.jsp                          --> LoadPreferences                   --> layout.jsp 2
/preferences                        --> LoadPreferences                   --> preferencesummary.jsp 3
/OrderController/SaveHistory.action --> SaveHistory                       --> page<operation variable> 4
/index.htm                          --> LoadCatalog                       --> index.jsp 5

1

Web client calls to /OrderController/LoadHistory.action will invoke the LoadHistory operation and the response will be rendered by history.jsp. A convention used by Skyway Builder is to automatically create a URL for new operations using the model package for namespacing the URL (i.e."/org/myapp/web//ControllerName/OperationName.action"). This can be overridden from the operation wizard when the operation is created, and it can also be overriden after the fact in the URL mapping tab.

2

Web client calls to /index.jsp will invoke the LoadPreferences operation and the response will be rendered by layout.jsp.

3

Web client calls to /preferences will invoke the LoadPreferences operation and the response will be rendered by preferencesummary.jsp. The point of this example is that you can have multiple URLs mapped to the same operation, and the same or separate View can be specified.

4

Web client calls to /OrderController/SaveHistory.action will invoke the SaveHistory operation and the response will be rendered by the view specified in the operation variable called page. The developer can dynamically specify the page based on the result of the operation.

5

Web client calls to /index.htm will invoke the LoadCatalogy operation and the response will be rendered by index.jsp. See Hiding the Implementation Technology for more details on this BEST PRACTICE

RELATED RECIPES

  1. Creating Helper Methods using Operations

  2. Reusing Operations in Different Contexts

  3. Hiding the Implementation Technology

  4. Implementing Post/Redirect/Get (PRG) Pattern