KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > mvc > AbstractCommandController


1 /*
2  * Copyright 2002-2007 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
17 package org.springframework.web.servlet.mvc;
18
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21
22 import org.springframework.validation.BindException;
23 import org.springframework.web.bind.ServletRequestDataBinder;
24 import org.springframework.web.servlet.ModelAndView;
25
26 /**
27  * Abstract base class for custom command controllers.
28  *
29  * <p>Autopopulates a command bean from the request. For command validation,
30  * a validator (property inherited from {@link BaseCommandController}) can be
31  * used.
32  *
33  * <p>In most cases this command controller should not be used to handle form
34  * submission, because functionality for forms is offered in more detail by the
35  * {@link org.springframework.web.servlet.mvc.AbstractFormController} and its
36  * corresponding implementations.
37  *
38  * <p><b><a name="config">Exposed configuration properties</a>
39  * (<a HREF="BaseCommandController.html#config">and those defined by superclass</a>):</b><br>
40  * <i>none</i> (so only those available in superclass).</p>
41  *
42  * <p><b><a name="workflow">Workflow
43  * (<a name="BaseCommandController.html#workflow">and that defined by superclass</a>):</b><br>
44  *
45  * @author Rod Johnson
46  * @author Juergen Hoeller
47  * @see #setCommandClass
48  * @see #setCommandName
49  * @see #setValidator
50  */

51 public abstract class AbstractCommandController extends BaseCommandController {
52
53     /**
54      * Create a new AbstractCommandController.
55      */

56     public AbstractCommandController() {
57     }
58
59     /**
60      * Create a new AbstractCommandController.
61      * @param commandClass class of the command bean
62      */

63     public AbstractCommandController(Class JavaDoc commandClass) {
64         setCommandClass(commandClass);
65     }
66
67     /**
68      * Create a new AbstractCommandController.
69      * @param commandClass class of the command bean
70      * @param commandName name of the command bean
71      */

72     public AbstractCommandController(Class JavaDoc commandClass, String JavaDoc commandName) {
73         setCommandClass(commandClass);
74         setCommandName(commandName);
75     }
76
77
78     protected ModelAndView handleRequestInternal(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
79             throws Exception JavaDoc {
80
81         Object JavaDoc command = getCommand(request);
82         ServletRequestDataBinder binder = bindAndValidate(request, command);
83         BindException errors = new BindException(binder.getBindingResult());
84         return handle(request, response, command, errors);
85     }
86
87     /**
88      * Template method for request handling, providing a populated and validated instance
89      * of the command class, and an Errors object containing binding and validation errors.
90      * <p>Call <code>errors.getModel()</code> to populate the ModelAndView model
91      * with the command and the Errors instance, under the specified command name,
92      * as expected by the "spring:bind" tag.
93      * @param request current HTTP request
94      * @param response current HTTP response
95      * @param command the populated command object
96      * @param errors validation errors holder
97      * @return a ModelAndView to render, or <code>null</code> if handled directly
98      * @see org.springframework.validation.Errors
99      * @see org.springframework.validation.BindException#getModel
100      */

101     protected abstract ModelAndView handle(
102             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Object JavaDoc command, BindException errors)
103             throws Exception JavaDoc;
104
105 }
106
Popular Tags