12. Reusing Operations in Different Contexts

PROBLEM

An operation is a specific set of logic that handles a user-generated event. There are occasions where an Operation may be reused in different contexts. There needs to be a strategy for reusing an Operation in a different context and associating it with a different View.

SOLUTION

A controller allows for multipe URL mapping entries to the same Operation. As the long as the URL is unique, addition URL mappings can be added to the controller and configured to the same Operation. In this scenario the developer mat also want to specify a different View for the Action.

HOW IT WORKS

The following two URL mappings are configured for the same operation but different views.

Example 2.5. URL Mapping - Reusing Actions

URL                                           OPERATION                             VIEW 
----------------------------------            --------------------------------      ---------------------------------
/OrderController/LoadHistory/detailed     --> LoadHistory                       --> detailview.jsp 1
/OrderController/LoadHistory/summary      --> LoadHistory                       --> summaryview.jsp 2
          

1

Web client calls to /OrderController/LoadHistory/detailed will invoke the LoadHistory operation and the response will be rendered by detailview.jsp.

2

Web client calls to /OrderController/LoadHistory/summary will invoke the LoadHistory operation and the response will be rendered by summaryview.jsp.

In both cases the LoadHistory operation is being invoked, however a different URL and View are specified in the URL Mapping.

RELATED RECIPES

  1. Mapping URLs to Operations and Views

  2. Creating Helper Methods using Operations

  3. Hiding the Implementation Technology

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