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