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.validation.BindException;
023: import org.springframework.web.bind.ServletRequestDataBinder;
024: import org.springframework.web.servlet.ModelAndView;
025:
026: /**
027: * Abstract base class for custom command controllers.
028: *
029: * <p>Autopopulates a command bean from the request. For command validation,
030: * a validator (property inherited from {@link BaseCommandController}) can be
031: * used.
032: *
033: * <p>In most cases this command controller should not be used to handle form
034: * submission, because functionality for forms is offered in more detail by the
035: * {@link org.springframework.web.servlet.mvc.AbstractFormController} and its
036: * corresponding implementations.
037: *
038: * <p><b><a name="config">Exposed configuration properties</a>
039: * (<a href="BaseCommandController.html#config">and those defined by superclass</a>):</b><br>
040: * <i>none</i> (so only those available in superclass).</p>
041: *
042: * <p><b><a name="workflow">Workflow
043: * (<a name="BaseCommandController.html#workflow">and that defined by superclass</a>):</b><br>
044: *
045: * @author Rod Johnson
046: * @author Juergen Hoeller
047: * @see #setCommandClass
048: * @see #setCommandName
049: * @see #setValidator
050: */
051: public abstract class AbstractCommandController extends
052: BaseCommandController {
053:
054: /**
055: * Create a new AbstractCommandController.
056: */
057: public AbstractCommandController() {
058: }
059:
060: /**
061: * Create a new AbstractCommandController.
062: * @param commandClass class of the command bean
063: */
064: public AbstractCommandController(Class commandClass) {
065: setCommandClass(commandClass);
066: }
067:
068: /**
069: * Create a new AbstractCommandController.
070: * @param commandClass class of the command bean
071: * @param commandName name of the command bean
072: */
073: public AbstractCommandController(Class commandClass,
074: String commandName) {
075: setCommandClass(commandClass);
076: setCommandName(commandName);
077: }
078:
079: protected ModelAndView handleRequestInternal(
080: HttpServletRequest request, HttpServletResponse response)
081: throws Exception {
082:
083: Object command = getCommand(request);
084: ServletRequestDataBinder binder = bindAndValidate(request,
085: command);
086: BindException errors = new BindException(binder
087: .getBindingResult());
088: return handle(request, response, command, errors);
089: }
090:
091: /**
092: * Template method for request handling, providing a populated and validated instance
093: * of the command class, and an Errors object containing binding and validation errors.
094: * <p>Call <code>errors.getModel()</code> to populate the ModelAndView model
095: * with the command and the Errors instance, under the specified command name,
096: * as expected by the "spring:bind" tag.
097: * @param request current HTTP request
098: * @param response current HTTP response
099: * @param command the populated command object
100: * @param errors validation errors holder
101: * @return a ModelAndView to render, or <code>null</code> if handled directly
102: * @see org.springframework.validation.Errors
103: * @see org.springframework.validation.BindException#getModel
104: */
105: protected abstract ModelAndView handle(HttpServletRequest request,
106: HttpServletResponse response, Object command,
107: BindException errors) throws Exception;
108:
109: }
|