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