001/*
002 * Copyright 2002-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.servlet.mvc;
018
019import javax.servlet.http.HttpServletRequest;
020import javax.servlet.http.HttpServletResponse;
021
022import org.springframework.web.servlet.ModelAndView;
023
024/**
025 * Base Controller interface, representing a component that receives
026 * {@code HttpServletRequest} and {@code HttpServletResponse}
027 * instances just like a {@code HttpServlet} but is able to
028 * participate in an MVC workflow. Controllers are comparable to the
029 * notion of a Struts {@code Action}.
030 *
031 * <p>Any implementation of the Controller interface should be a
032 * <i>reusable, thread-safe</i> class, capable of handling multiple
033 * HTTP requests throughout the lifecycle of an application. To be able to
034 * configure a Controller easily, Controller implementations are encouraged
035 * to be (and usually are) JavaBeans.
036 *
037 * <h3><a name="workflow">Workflow</a></h3>
038 *
039 * <p>After a {@code DispatcherServlet} has received a request and has
040 * done its work to resolve locales, themes, and suchlike, it then tries
041 * to resolve a Controller, using a
042 * {@link org.springframework.web.servlet.HandlerMapping HandlerMapping}.
043 * When a Controller has been found to handle the request, the
044 * {@link #handleRequest(HttpServletRequest, HttpServletResponse) handleRequest}
045 * method of the located Controller will be invoked; the located Controller
046 * is then responsible for handling the actual request and &mdash; if applicable
047 * &mdash; returning an appropriate
048 * {@link org.springframework.web.servlet.ModelAndView ModelAndView}.
049 * So actually, this method is the main entry point for the
050 * {@link org.springframework.web.servlet.DispatcherServlet DispatcherServlet}
051 * which delegates requests to controllers.
052 *
053 * <p>So basically any <i>direct</i> implementation of the {@code Controller} interface
054 * just handles HttpServletRequests and should return a ModelAndView, to be further
055 * interpreted by the DispatcherServlet. Any additional functionality such as
056 * optional validation, form handling, etc. should be obtained through extending
057 * {@link org.springframework.web.servlet.mvc.AbstractController AbstractController}
058 * or one of its subclasses.
059 *
060 * <h3>Notes on design and testing</h3>
061 *
062 * <p>The Controller interface is explicitly designed to operate on HttpServletRequest
063 * and HttpServletResponse objects, just like an HttpServlet. It does not aim to
064 * decouple itself from the Servlet API, in contrast to, for example, WebWork, JSF or Tapestry.
065 * Instead, the full power of the Servlet API is available, allowing Controllers to be
066 * general-purpose: a Controller is able to not only handle web user interface
067 * requests but also to process remoting protocols or to generate reports on demand.
068 *
069 * <p>Controllers can easily be tested by passing in mock objects for the
070 * HttpServletRequest and HttpServletResponse objects as parameters to the
071 * {@link #handleRequest(HttpServletRequest, HttpServletResponse) handleRequest}
072 * method. As a convenience, Spring ships with a set of Servlet API mocks
073 * that are suitable for testing any kind of web components, but are particularly
074 * suitable for testing Spring web controllers. In contrast to a Struts Action,
075 * there is no need to mock the ActionServlet or any other infrastructure;
076 * mocking HttpServletRequest and HttpServletResponse is sufficient.
077 *
078 * <p>If Controllers need to be aware of specific environment references, they can
079 * choose to implement specific awareness interfaces, just like any other bean in a
080 * Spring (web) application context can do, for example:
081 * <ul>
082 * <li>{@code org.springframework.context.ApplicationContextAware}</li>
083 * <li>{@code org.springframework.context.ResourceLoaderAware}</li>
084 * <li>{@code org.springframework.web.context.ServletContextAware}</li>
085 * </ul>
086 *
087 * <p>Such environment references can easily be passed in testing environments,
088 * through the corresponding setters defined in the respective awareness interfaces.
089 * In general, it is recommended to keep the dependencies as minimal as possible:
090 * for example, if all you need is resource loading, implement ResourceLoaderAware only.
091 * Alternatively, derive from the WebApplicationObjectSupport base class, which gives
092 * you all those references through convenient accessors but requires an
093 * ApplicationContext reference on initialization.
094 *
095 * <p>Controllers can optionally implement the {@link LastModified} interface.
096 *
097 * @author Rod Johnson
098 * @author Juergen Hoeller
099 * @see LastModified
100 * @see SimpleControllerHandlerAdapter
101 * @see AbstractController
102 * @see org.springframework.mock.web.MockHttpServletRequest
103 * @see org.springframework.mock.web.MockHttpServletResponse
104 * @see org.springframework.context.ApplicationContextAware
105 * @see org.springframework.context.ResourceLoaderAware
106 * @see org.springframework.web.context.ServletContextAware
107 * @see org.springframework.web.context.support.WebApplicationObjectSupport
108 */
109public interface Controller {
110
111        /**
112         * Process the request and return a ModelAndView object which the DispatcherServlet
113         * will render. A {@code null} return value is not an error: it indicates that
114         * this object completed request processing itself and that there is therefore no
115         * ModelAndView to render.
116         * @param request current HTTP request
117         * @param response current HTTP response
118         * @return a ModelAndView to render, or {@code null} if handled directly
119         * @throws Exception in case of errors
120         */
121        ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception;
122
123}