Java Reference
In-Depth Information
As shown in Listing 3-16, the
CreatePolicyController
class implements the
Controller
interface provided by the Spring Framework. Hence, it implements the
handleRequest
method of this interface. This method uses the data in the
HttpServletRequest
to
populate a simple JavaBean object
PolicyDetail
. It then invokes the business operation
to create a new policy. The business logic is invoked using the client-side facade
UnderwritingBusinessDelegate
, which implements the business delegate pattern
described in Chapter 4. As shown in Listing 3-17, the business delegate object is injected
by the Spring container. Finally, the
ModelAndView
object containing the logical view name
and data returned by the business layer is passed to the handler adapter, which invoked
this page controller.
Listing 3-17.
Spring-config.xml
<beans xmlns="
http://www.springframework.org/schema/beans"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
<bean name="/createPolicy.do" class="com.apress.insuranceapp.web.controller
å
.CreatePolicyController">
<property name="uwrBusinessDelegate" ref="uwrBusinessDelegate"/>
</bean>
<bean name="uwrBusinessDelegate" class="com.apress.insuranceapp.business.
å
delegate.UnderwritingBusinessDelegateImpl"/>
</beans>
The classes that implement the controller interface must be thread-safe, because
they are singleton by default. The controllers have complete access to
HttpServletRequest
and
HttpServletResponse
objects, thus making them dependent on the HTTP protocol.
But this also makes these components usable by remoting mechanisms that depend on
HTTP. You can use
ThrowawayController
if the target is independent from the servlet API
and the controller does not need to be thread-safe.
Using AbstractController
For most cases, the implementation of the
Controller
interface is sufficient. Spring, how-
ever, provides several concrete as well as abstract implementations that can be extended
depending on the requirement. The class diagram in Figure 3-8 shows one such class.
