Choosing a Controller for your work in Spring MVC

July 24, 2008

Spring provides good number of well structured Controller to work with Spring MVC and work flow. If you are familiar with Spring frame work, you must have work with two master piece controller SimpleFormControllerand and AbstractController. One for only view read-only request and other one includes full form handling mechanism. But this are not the end of the world. There are several Controllers that one can choose for various purpose.

AbstractWizardFormController Think about a form thats large in length. User have to scroll down dipper to fill up all the necessary fields of a form. Suppose, you are filling a long form to be a member of a job site, where there are sections like Name and Address, Job experience, Educational back grounds, Achievements e.t.c. But if all those thing bind with a single been then how about Splits this form across multiple pages? If you really want to do some thing like this, this is the controller for you :D

MultiActionController You are working with Spring MVC not in Web-Flow. But some time it needs to handle more work flow than a single one. Well though its possible to do the same task with AbstractFormController but the number of Controller will be more. For handling multiple action, this controller provides three different method:
public ModelAndView doStuff(HttpServletRequest req,
HttpServletResponse res) { … }
public ModelAndView doOtherStuff(HttpServletRequest req,
HttpServletResponse res, HttpSession session) { … }
public ModelAndView doStuff(HttpServletRequest req,
HttpServletResponse res, CommandBean command) { … }

One can choose any one of those on his requirement. But the basic problem with this Controller is it does not provide any work flow. All of this must be done by manually.

ThrowawayController The flavour of Command Pattern comes with this controller in Spring MVC. If you do not like to work with request/response stateless controller. The controller itself work as a command bean. Non-singleton. Lack of validation enforce another version of the controller named ValidatableThrowawayController.

By: Md. Shahjalal


Note on ThrowawayController of Spring MVC

July 20, 2008

ThrowawayController(org.springframework.web.servlet.mvc.throwaway.ThrowawayController) of Spring MVC is an alternative for request/response-type web framework who do not wish to work with stateless singleton controllers(like SimpleFormController) with servlet request/response. For request handling this controller uses the command pattern. ThrowawayController it self works as the command bean and request handler which have state and behavior. Yes as you guess, this must be a prototype. In the configuration, you must make sure this thing by setting singletone=”false”.

<bean name="/
sampleThroughwayController" singleton="false"
class="com.project.web.SampleThroughwayController">
</bean>

The most dangerous thing about this controller is shown up by not setting this property. Neither the DispatcherServlet nor ThrowawayControllerHandlerAdapter will generate any warning for this. So, be careful to check this property before you send your code for production.

One must include ThrowawayControllerHandlerAdapter in WebApplicationContext to run this. Though by default the DispatherServlet include ThrowawayControllerHandlerAdapter but it will not work until a single ThrowawayControllerHandlerAdapter is declared implicitly in WebApplicationContext.

Not including any Servlet api with this class gives us advantage on testing. One will not need to create a MockHttpServletRequest to test this class. When you need to consider the request as a true command bean, this is the best choice for the coder in Spring MVC. There is no need to access the HttpServletRequest, HttpServletResponse classes.

ThrowawayController do not implement form work flow, so for handling validation or error it is not a good choice. But the most hassle a programmer have to face when one works with data binding. ThrowawayController do not have any customize data binding. To solve the problem Spring MVC provides ValidatableThrowawayController that supports custom PropertyEditors. oh dont forget to configure ValidatableThrowawayControllerHandlerAdapter in your WebApplicationContext as ThrowawayControllerHandlerAdapter.


Follow

Get every new post delivered to your Inbox.