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