1 /* 2 * Copyright 2002-2012 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 17 package org.springframework.web.servlet; 18 19 import java.util.Map; 20 import javax.servlet.http.HttpServletRequest; 21 import javax.servlet.http.HttpServletResponse; 22 23 import org.springframework.http.MediaType; 24 25 /** 26 * MVC View for a web interaction. Implementations are responsible for rendering 27 * content, and exposing the model. A single view exposes multiple model attributes. 28 * 29 * <p>This class and the MVC approach associated with it is discussed in Chapter 12 of 30 * <a href="http://www.amazon.com/exec/obidos/tg/detail/-/0764543857/">Expert One-On-One J2EE Design and Development</a> 31 * by Rod Johnson (Wrox, 2002). 32 * 33 * <p>View implementations may differ widely. An obvious implementation would be 34 * JSP-based. Other implementations might be XSLT-based, or use an HTML generation library. 35 * This interface is designed to avoid restricting the range of possible implementations. 36 * 37 * <p>Views should be beans. They are likely to be instantiated as beans by a ViewResolver. 38 * As this interface is stateless, view implementations should be thread-safe. 39 * 40 * @author Rod Johnson 41 * @author Arjen Poutsma 42 * @see org.springframework.web.servlet.view.AbstractView 43 * @see org.springframework.web.servlet.view.InternalResourceView 44 */ 45 public interface View { 46 47 /** 48 * Name of the {@link HttpServletRequest} attribute that contains the response status code. 49 * <p>Note: This attribute is not required to be supported by all View implementations. 50 */ 51 String RESPONSE_STATUS_ATTRIBUTE = View.class.getName() + ".responseStatus"; 52 53 /** 54 * Name of the {@link HttpServletRequest} attribute that contains a Map with path variables. 55 * The map consists of String-based URI template variable names as keys and their corresponding 56 * Object-based values -- extracted from segments of the URL and type converted. 57 * 58 * <p>Note: This attribute is not required to be supported by all View implementations. 59 */ 60 String PATH_VARIABLES = View.class.getName() + ".pathVariables"; 61 62 /** 63 * The {@link MediaType} selected during content negotiation, which may be 64 * more specific than the one the View is configured with. For example: 65 * "application/vnd.example-v1+xml" vs "application/*+xml". 66 */ 67 String SELECTED_CONTENT_TYPE = View.class.getName() + ".selectedContentType"; 68 69 /** 70 * Return the content type of the view, if predetermined. 71 * <p>Can be used to check the content type upfront, 72 * before the actual rendering process. 73 * @return the content type String (optionally including a character set), 74 * or {@code null} if not predetermined. 75 */ 76 String getContentType(); 77 78 /** 79 * Render the view given the specified model. 80 * <p>The first step will be preparing the request: In the JSP case, 81 * this would mean setting model objects as request attributes. 82 * The second step will be the actual rendering of the view, 83 * for example including the JSP via a RequestDispatcher. 84 * @param model Map with name Strings as keys and corresponding model 85 * objects as values (Map can also be {@code null} in case of empty model) 86 * @param request current HTTP request 87 * @param response HTTP response we are building 88 * @throws Exception if rendering failed 89 */ 90 void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception; 91 92 }