Java Reference
In-Depth Information
Thus, a throwaway controller is a combination of form bean and page controller.
Note that its independence from the Servlet API increases reusability and makes this con-
troller easier to unit test. But it has some disadvantages too. Since this is a stateful
controller, a new instance should be created for each request. This in turn wastes space
on the JVM heap, increasing the need for garbage collection and the resulting pauses.
However, with a modern well-tuned JVM, this is not a major problem. This controller is
very simple, without any detailed workflow. It does not support validation of form fields.
Moreover, since the form fields are now part of this class, it is awkward to pass data from
presentation layer to business layer.
The tight coupling of the form bean in the page controller can be solved by a custom
solution. To achieve this, I will define a new throwaway controller interface that, like the
earlier one, is free from servlet API dependency. This is shown in Listing 3-37.
Listing 3-37. SimpleFormThrowawayController.java
package com.apress.insurance.web.controller.api;
import org.springframework.web.servlet.ModelAndView;
public interface SimpleFormThrowawayController {
public ModelAndView execute(Object formBean) throws Exception;
public Class getFormbeanClass();
}
Note that the throwaway controllers now need to implement this new interface, as
shown in Listing 3-37. The execute method of this interface needs to be implemented to
invoke business logic. It receives an instance of the form bean from the handler adapter.
Now I will show an implementation of the new throwaway controller and move the func-
tionality shown in Listing 3-34 to this one.
It is evident from Listing 3-38 that this throwaway controller, being stateless, can
have just a single instance in the web application context.
Listing 3-38. SaveClaimController.java
public class SaveClaimController implements SimpleFormThrowawayController {
public ModelAndView execute(Object formBean) throws Exception {
ClaimFormbean formbean = (ClaimFormbean)formBean;
//Invoke business logic here
return new ModelAndView("claimDetails");
Search WWH ::




Custom Search