001/* 002 * Copyright 2002-2017 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.config.annotation; 018 019import org.springframework.context.ApplicationContext; 020import org.springframework.http.HttpStatus; 021import org.springframework.lang.Nullable; 022import org.springframework.util.Assert; 023import org.springframework.web.servlet.RequestToViewNameTranslator; 024import org.springframework.web.servlet.mvc.ParameterizableViewController; 025 026/** 027 * Assist with the registration of a single view controller. 028 * 029 * @author Rossen Stoyanchev 030 * @author Keith Donald 031 * @since 3.1 032 */ 033public class ViewControllerRegistration { 034 035 private final String urlPath; 036 037 private final ParameterizableViewController controller = new ParameterizableViewController(); 038 039 040 public ViewControllerRegistration(String urlPath) { 041 Assert.notNull(urlPath, "'urlPath' is required."); 042 this.urlPath = urlPath; 043 } 044 045 046 /** 047 * Set the status code to set on the response. Optional. 048 * <p>If not set the response status will be 200 (OK). 049 */ 050 public ViewControllerRegistration setStatusCode(HttpStatus statusCode) { 051 this.controller.setStatusCode(statusCode); 052 return this; 053 } 054 055 /** 056 * Set the view name to return. Optional. 057 * <p>If not specified, the view controller will return {@code null} as the 058 * view name in which case the configured {@link RequestToViewNameTranslator} 059 * will select the view name. The {@code DefaultRequestToViewNameTranslator} 060 * for example translates "/foo/bar" to "foo/bar". 061 * @see org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator 062 */ 063 public void setViewName(String viewName) { 064 this.controller.setViewName(viewName); 065 } 066 067 protected void setApplicationContext(@Nullable ApplicationContext applicationContext) { 068 this.controller.setApplicationContext(applicationContext); 069 } 070 071 protected String getUrlPath() { 072 return this.urlPath; 073 } 074 075 protected ParameterizableViewController getViewController() { 076 return this.controller; 077 } 078 079}