1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.web.servlet.mvc;
  17. import javax.servlet.http.HttpServletRequest;
  18. import javax.servlet.http.HttpServletResponse;
  19. import org.springframework.web.servlet.ModelAndView;
  20. /**
  21. * Base Controller interface, representing a component that receives HttpServletRequest
  22. * and HttpServletResponse like a <code>HttpServlet</code> but is able to participate in
  23. * an MVC workflow. Comparable to the notion of a Struts <code>Action</code>
  24. *
  25. * <p>Any implementation of the Controller interface should be a
  26. * <i>reusable, threadsafe</i> class, capable of handling multiple
  27. * HTTP requests throughout the lifecycle of an application. To be able to
  28. * configure Controller in an easy, Controllers are usually JavaBeans.</p>
  29. *
  30. * <p><b><a name="workflow">Workflow</a>:</b><br>
  31. * After the DispatcherServlet has received a request and has done its work
  32. * to resolve locales, themes and things a like, it tries to resolve
  33. * a Controller, using a {@link org.springframework.web.servlet.HandlerMapping
  34. * HandlerMapping}. When a Controller has been found, the \
  35. * {@link #handleRequest(HttpServletRequest, HttpServletResponse) handleRequest()}
  36. * method will be invoked, which is responsible for handling the actual
  37. * request and - if applicable - returning an appropriate ModelAndView.
  38. * So actually, this method is the main entrypoint for the
  39. * {@link org.springframework.web.servlet.DispatcherServlet DispatcherServlet}
  40. * which delegates requests to controllers. This method - and also this interface -
  41. * should preferrably not be implemented by custom controllers <i>directly</i>, since
  42. * abstract controller also provided by this package already provide a lot
  43. * of functionality common for virtually all webapplications. A few examples of
  44. * those abstract controllers:
  45. * {@link AbstractController AbstractController},
  46. * {@link AbstractCommandController AbstractCommandController} and
  47. * {@link AbstractFormController AbstractFormController}.
  48. * </p>
  49. * <p>So basically any <i>direct</i> implementation of the Controller interface
  50. * just handles HttpServletRequests and should return a ModelAndView, to be
  51. * further used by the DispatcherServlet. Any additional functionality such
  52. * as optional validation, formhandling, etcetera should be obtained by
  53. * extended one of the abstract controller mentioned above.
  54. * </p>
  55. *
  56. * @author Rod Johnson
  57. * @version $Id: Controller.java,v 1.5 2004/03/18 02:46:08 trisberg Exp $
  58. * @see SimpleControllerHandlerAdapter
  59. */
  60. public interface Controller {
  61. /**
  62. * Process the request and return a ModelAndView object which the DispatcherServlet
  63. * will render. A null return is not an error: It indicates that this object
  64. * completed request processing itself, thus there is no ModelAndView to render.
  65. * @param request current HTTP request
  66. * @param response current HTTP response
  67. * @return a ModelAndView to render, or null if handled directly
  68. * @throws Exception in case of errors
  69. */
  70. ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception;
  71. }