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
Parameter | Description | |
---|---|---|
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:
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
/index.jsp --> LoadPreferences --> layout.jsp
/preferences --> LoadPreferences --> preferencesummary.jsp
/OrderController/SaveHistory.action --> SaveHistory --> page<operation variable>
/index.htm --> LoadCatalog --> index.jsp
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. | |
Web client calls to
/index.jsp will invoke the
LoadPreferences operation and the
response will be rendered by
layout.jsp. | |
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. | |
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. | |
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