File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes Difference between handleRequest() and handle() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Difference between handleRequest() and handle()" Watch "Difference between handleRequest() and handle()" New topic
Author

Difference between handleRequest() and handle()

pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Hi,

Could you please tell me the difference between handleRequest() and handle().

The Javadoc of the Controller interface is as follows :
After a DispatcherServlet has received a request and has done its work to resolve locales, themes and suchlike, it then tries to resolve a Controller, using a org.springframework.web.servlet.HandlerMapping.
When a Controller has been found to handle the request, the handleRequest(HttpServletRequest, HttpServletResponse) method of the located Controller will be invoked; the located Controller is then responsible for handling the actual request and - if applicable - returning an appropriate org.springframework.web.servlet.ModelAndView. So actually, this method is the main entrypoint for the org.springframework.web.servlet.DispatcherServlet which delegates requests to controllers.

I have read in an article that in order to execute HandlerExecution, HandlerAdapter is required which contains the handle() method.

Please advise.


Thanks,
Pramod
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Well first I have to point out today that a Controller does NOT have to return a ModelAndView.

OK, think of it this way. A Controller method can have any signature, and you want it to be as POJO as you like. But some of the parameter values from the Request.

So we need a couple of things to happen before the controller method can be called. This happens after the Dispatcher has received the request

1) Figure out which Controller and what method in it to call. That is the responsibility of a Mapping Handler.
2) Convert the data from the request into the correct formats/types for that controller method. And maybe other places of data that needs converted before calling the Controller method. That is the responsibility of a HandlerAdapter.

Hope that helps

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Hi Mark,

So does the handle() method of the HandlerAdapter calls the handleRequest() of the controller to execute HandlerExecutionChain as both methods return either ModelAndView or null as per javadoc.
Does the 2nd step you mentioned get executed in the handle() method ?


Thanks for the reply.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

pramod talekar wrote:Hi Mark,

So does the handle() method of the HandlerAdapter calls the handleRequest() of the controller to execute HandlerExecutionChain as both methods return either ModelAndView or null as per javadoc.
Does the 2nd step you mentioned get executed in the handle() method ?


Thanks for the reply.


Well it depends on what version or how you are using Spring MVC. If you are using an older version of Spring, or your code is implementing the Controller interface (which to me is doing Spring MVC the old old way) then there is a handleRequest method in the controller interface. If you are not doing it the old way, then you will have a pojo method with @RequestMapping on it. So the method might be called listAccounts() or accountDetails(@RequestParam("accountId") String accountId)

Mark
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Right Mark. I need to use the Controller interface.
2) Convert the data from the request into the correct formats/types for that controller method. And maybe other places of data that needs converted before calling the Controller method. That is the responsibility of a HandlerAdapter.


So, Once the controller and method has been selected by HandlerMapping, the DispatcherServlet calls the HandlerAdapter for the above step which has also a ModelAndView in its method's signature and then the controller is executed. Right?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Well, if you are implementing the Controller interface, then I really can't be of much help to you. That is the old way of doing things, and in the past 5 years of doing Spring I have never ever implemented that interface for my Spring MVC applications. I don't want my code coupled with Spring, so I use @Controller and @RequestMapping on POJO classes.

Mark
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Thanks a lot Mark.
Just for curiosity, do you use web.xml or WebApplicationInitializer ?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

pramod talekar wrote:Thanks a lot Mark.
Just for curiosity, do you use web.xml or WebApplicationInitializer ?


I have always used web.xml. But the Initializer is new and I really like that it removes the last bit of xml. I suggest using WebApplicationInitializer if you can, as well as using Java Based Configuration with @Configuration/@Bean as well as using @Component and stereotypes in your code.

Of course, that is my personal preference and isn't law. ;)

Mark
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Thanks Mark.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Difference between handleRequest() and handle()
 
Similar Threads
what objects can we return from the methods of controller?
populate a form
forward and sendRedirect
What is the difference between using @RequestMapping before class and before method
java.util. Missing Resource Exception