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.validation.BindException;
  20. import org.springframework.web.bind.ServletRequestDataBinder;
  21. import org.springframework.web.servlet.ModelAndView;
  22. /**
  23. * <p>Abstract base class for custom command controllers. Autopopulates a
  24. * command bean from the request. For command validation, a validator
  25. * (property inherited from BaseCommandController) can be used.</p>
  26. *
  27. * <p>This command controller should preferrable not be used to handle form
  28. * submission, because functionality for forms is more offered in more
  29. * detail by the {@link org.springframework.web.servlet.mvc.AbstractFormController
  30. * AbstractFormController} and its corresponding implementations.</p>
  31. *
  32. * <p><b><a name="config">Exposed configuration properties</a>
  33. * (<a href="AbstractController.html#config">and those defined by superclass</a>):</b><br>
  34. * <i>none</i> (so only those available in superclass).</p>
  35. *
  36. * <p><b><a name="workflow">Workflow
  37. * (<a name="BaseCommandController.html#workflow">and that defined by superclass</a>):</b><br>
  38. *
  39. * @author Rod Johnson
  40. * @author Juergen Hoeller
  41. * @see #setCommandClass
  42. * @see #setCommandName
  43. * @see #setValidator
  44. */
  45. public abstract class AbstractCommandController extends BaseCommandController {
  46. /**
  47. * Create a new AbstractCommandController.
  48. */
  49. public AbstractCommandController() {
  50. }
  51. /**
  52. * Create a new AbstractCommandController.
  53. * @param commandClass class of the command bean
  54. */
  55. public AbstractCommandController(Class commandClass) {
  56. setCommandClass(commandClass);
  57. }
  58. /**
  59. * Create a new AbstractCommandController.
  60. * @param commandClass class of the command bean
  61. * @param commandName name of the command bean
  62. */
  63. public AbstractCommandController(Class commandClass, String commandName) {
  64. setCommandClass(commandClass);
  65. setCommandName(commandName);
  66. }
  67. protected final ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
  68. throws Exception {
  69. Object command = getCommand(request);
  70. ServletRequestDataBinder binder = bindAndValidate(request, command);
  71. return handle(request, response, command, binder.getErrors());
  72. }
  73. /**
  74. * Template method for request handling, providing a populated and validated instance
  75. * of the command class, and an Errors object containing binding and validation errors.
  76. * <p>Call <code>errors.getModel()</code> to populate the ModelAndView model
  77. * with the command and the Errors instance, under the specified command name,
  78. * as expected by the "spring:bind" tag.
  79. * @param request current HTTP request
  80. * @param response current HTTP response
  81. * @param command the populated command object
  82. * @param errors validation errors holder
  83. * @return a ModelAndView to render, or null if handled directly
  84. * @see org.springframework.validation.Errors
  85. * @see org.springframework.validation.BindException#getModel
  86. */
  87. protected abstract ModelAndView handle(HttpServletRequest request, HttpServletResponse response,
  88. Object command, BindException errors) throws Exception;
  89. }