001/*
002 * Copyright 2002-2014 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.portlet.mvc;
018
019import javax.portlet.RenderRequest;
020import javax.portlet.RenderResponse;
021
022import org.springframework.web.portlet.ModelAndView;
023
024/**
025 * Trivial controller that always returns a named view. The view can be configured
026 * using an exposed configuration property. This controller offers an alternative
027 * to sending a request straight to a view such as a JSP. The advantage here is
028 * that the client is not exposed to the concrete view technology but rather just
029 * to the controller URL; the concrete view will be determined by the ViewResolver.
030 *
031 * <p><b><a name="workflow">Workflow
032 * (<a href="AbstractController.html#workflow">and that defined by superclass</a>):</b>
033 * <ol>
034 * <li>Render request is received by the controller</li>
035 * <li>call to {@link #handleRenderRequestInternal handleRenderRequestInternal} which
036 * just returns the view, named by the configuration property {@code viewName}.</li>
037 * </ol>
038 *
039 * <p>This controller does not handle action requests.
040 *
041 * <p><b><a name="config">Exposed configuration properties</a>
042 * (<a href="AbstractController.html#config">and those defined by superclass</a>):</b>
043 * <table border="1">
044 * <tr>
045 * <td><b>name</b></td>
046 * <td><b>default</b></td>
047 * <td><b>description</b></td>
048 * </tr>
049 * <tr>
050 * <td>viewName</td>
051 * <td><i>null</i></td>
052 * <td>the name of the view the viewResolver will use to forward to (if this property
053 * is not set, an exception will be thrown during initialization)</td>
054 * </tr>
055 * </table>
056 *
057 * @author John A. Lewis
058 * @since 2.0
059 */
060public class ParameterizableViewController extends AbstractController {
061
062        private String viewName;
063
064
065        /**
066         * Set the name of the view to delegate to.
067         */
068        public void setViewName(String viewName) {
069                this.viewName = viewName;
070        }
071
072        /**
073         * Return the name of the view to delegate to.
074         */
075        public String getViewName() {
076                return this.viewName;
077        }
078
079        @Override
080        protected void initApplicationContext() {
081                if (this.viewName == null) {
082                        throw new IllegalArgumentException("Property 'viewName' is required");
083                }
084        }
085
086
087        /**
088         * Return a ModelAndView object with the specified view name.
089         */
090        @Override
091        protected ModelAndView handleRenderRequestInternal(RenderRequest request, RenderResponse response)
092                        throws Exception {
093
094                return new ModelAndView(getViewName());
095        }
096
097}