001/*
002 * Copyright 2002-2018 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;
018
019import java.util.Map;
020import javax.servlet.http.HttpServletRequest;
021import javax.servlet.http.HttpServletResponse;
022
023/**
024 * MVC View for a web interaction. Implementations are responsible for rendering
025 * content, and exposing the model. A single view exposes multiple model attributes.
026 *
027 * <p>This class and the MVC approach associated with it is discussed in Chapter 12 of
028 * <a href="https://www.amazon.com/exec/obidos/tg/detail/-/0764543857/">Expert One-On-One J2EE Design and Development</a>
029 * by Rod Johnson (Wrox, 2002).
030 *
031 * <p>View implementations may differ widely. An obvious implementation would be
032 * JSP-based. Other implementations might be XSLT-based, or use an HTML generation library.
033 * This interface is designed to avoid restricting the range of possible implementations.
034 *
035 * <p>Views should be beans. They are likely to be instantiated as beans by a ViewResolver.
036 * As this interface is stateless, view implementations should be thread-safe.
037 *
038 * @author Rod Johnson
039 * @author Arjen Poutsma
040 * @author Rossen Stoyanchev
041 * @see org.springframework.web.servlet.view.AbstractView
042 * @see org.springframework.web.servlet.view.InternalResourceView
043 */
044public interface View {
045
046        /**
047         * Name of the {@link HttpServletRequest} attribute that contains the response status code.
048         * <p>Note: This attribute is not required to be supported by all View implementations.
049         * @since 3.0
050         */
051        String RESPONSE_STATUS_ATTRIBUTE = View.class.getName() + ".responseStatus";
052
053        /**
054         * Name of the {@link HttpServletRequest} attribute that contains a Map with path variables.
055         * The map consists of String-based URI template variable names as keys and their corresponding
056         * Object-based values -- extracted from segments of the URL and type converted.
057         * <p>Note: This attribute is not required to be supported by all View implementations.
058         * @since 3.1
059         */
060        String PATH_VARIABLES = View.class.getName() + ".pathVariables";
061
062        /**
063         * The {@link org.springframework.http.MediaType} selected during content negotiation,
064         * which may be more specific than the one the View is configured with. For example:
065         * "application/vnd.example-v1+xml" vs "application/*+xml".
066         * @since 3.2
067         */
068        String SELECTED_CONTENT_TYPE = View.class.getName() + ".selectedContentType";
069
070
071        /**
072         * Return the content type of the view, if predetermined.
073         * <p>Can be used to check the view's content type upfront,
074         * i.e. before an actual rendering attempt.
075         * @return the content type String (optionally including a character set),
076         * or {@code null} if not predetermined
077         */
078        String getContentType();
079
080        /**
081         * Render the view given the specified model.
082         * <p>The first step will be preparing the request: In the JSP case, this would mean
083         * setting model objects as request attributes. The second step will be the actual
084         * rendering of the view, for example including the JSP via a RequestDispatcher.
085         * @param model Map with name Strings as keys and corresponding model
086         * objects as values (Map can also be {@code null} in case of empty model)
087         * @param request current HTTP request
088         * @param response HTTP response we are building
089         * @throws Exception if rendering failed
090         */
091        void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception;
092
093}