1. /*
  2. * Copyright 2002-2004 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. package org.springframework.web.servlet.mvc;
  17. import javax.servlet.http.HttpServletRequest;
  18. import javax.servlet.http.HttpServletResponse;
  19. import org.springframework.web.servlet.ModelAndView;
  20. /**
  21. * <p>Trivial controller that always returns a named view. The view
  22. * can be configured using an exposed configuration property. This
  23. * controller offers an alternative to sending a request straight to a view
  24. * such as a JSP. The advantage here is, that you're decoupling the controller
  25. * and the view, letter the some the configuration determine (instead of
  26. * the controller) the viewtechnology.</p>
  27. *
  28. * <p>An alternative to the ParameterizableViewController is of the
  29. * {@link org.springframework.web.servlet.mvc.multiaction MultiAction controllers},
  30. * some of which allow the same behavior, but then for more views at in one
  31. * controller.</p>
  32. *
  33. * <p><b><a name="workflow">Workflow
  34. * (<a href="AbstractController.html#workflow">and that defined by superclass</a>):</b><br>
  35. * <ol>
  36. * <li>Request is received by the controller</li>
  37. * <li>call to {@link #handleRequestInternal handleRequestInternal} which
  38. * just returns the view, named by the configuration property
  39. * <code>viewName</code>. Nothing more, nothing less</li>
  40. * </ol>
  41. * </p>
  42. *
  43. * <p><b><a name="config">Exposed configuration properties</a>
  44. * (<a href="AbstractController.html#config">and those defined by superclass</a>):</b><br>
  45. * <table border="1">
  46. * <tr>
  47. * <td><b>name</b></td>
  48. * <td><b>default</b></td>
  49. * <td><b>description</b></td>
  50. * </tr>
  51. * <tr>
  52. * <td>viewName</td>
  53. * <td><i>null</i></td>
  54. * <td>the name of the view the viewResolver will use to forward to
  55. * (if this property is not set, an exception will be thrown during
  56. * initialization)</td>
  57. * </tr>
  58. * </table>
  59. * </p>
  60. *
  61. * @author Rod Johnson
  62. */
  63. public class ParameterizableViewController extends AbstractController {
  64. private String viewName;
  65. /**
  66. * Set the name of the view to delegate to.
  67. */
  68. public void setViewName(String viewName) {
  69. this.viewName = viewName;
  70. }
  71. /**
  72. * Return the name of the view to delegate to.
  73. */
  74. public String getViewName() {
  75. return viewName;
  76. }
  77. protected void initApplicationContext() {
  78. if (this.viewName == null) {
  79. throw new IllegalArgumentException("viewName is required");
  80. }
  81. }
  82. /**
  83. * Return a ModelAndView object with the specified view name.
  84. */
  85. protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
  86. throws Exception {
  87. return new ModelAndView(this.viewName);
  88. }
  89. }