Java Reference
In-Depth Information
The
DispatcherServlet
uses the
BeanNameUrlHandlerMapping
class to map an incoming
request URL to the appropriate page controller that is going to process that request. The
handler mapping bean, however, is not configured in Listing 3-4. This is because Spring
assumes this as a sensible default. As we will see later, it is possible to use different logic
to map an incoming request to its handler by implementing the
HandlerMapping
interface.
I will discuss other
HandlerMapping
implementations provided by the Spring Framework
later in this chapter.
With the central request-handling gateway installed, it's time to refactor the JSP
described in Listing 3-1 to route all requests to the front controller. This is shown in
Listing 3-5.
Listing 3-5.
Policy.jsp
<title>Underwriting</title>
<script>
function eventSubmit(url){
document.uwr.action = url;
document.uwr.submit();
}
</script>
</head>
<body onLoad="displayError(<%=request.getAttribute("ERROR_MESSAGE")%>)">
<form action="" name="uwr">
Name of Insured <input type="text" value="" />
<br/>
<input type="submit" value="Create" onClick="eventSubmit('createPolicy.do')"/>
<input type="submit" value="Update" onClick="eventSubmit('updatePolicy.do')"/>
</form>
Note that the JSP no longer uses the event code and screen code. Instead, it now uses
logical request URLs. When a request for the URL
/createPolicy.do
reaches the front con-
troller, it uses the handler mapping to determine whether a page controller has been
registered to process this request. The processing is then delegated to the appropriate
page controller if one is registered; otherwise, an error is raised. In this case, the process-
ing is carried out by the
CreatePolicyController
. The simplest controller would implement
the
handleRequest
method of the
org.springframework.web.servlet.mvc.Controller
inter-
face. The
handleRequest
method should have most of the code that was in the
if-else
block of the JSP controller. Simply stated, this controller will take care of the code in the
if-else
blocks. After the business components are invoked and page controllers return,
