KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2006 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.portlet.mvc;
18
19 import javax.portlet.ActionRequest;
20 import javax.portlet.ActionResponse;
21 import javax.portlet.RenderRequest;
22 import javax.portlet.RenderResponse;
23
24 import org.springframework.validation.BindException;
25 import org.springframework.web.portlet.ModelAndView;
26 import org.springframework.web.portlet.bind.PortletRequestDataBinder;
27
28 /**
29  * <p>Abstract base class for custom command controllers. Autopopulates a
30  * command bean from the request. For command validation, a validator
31  * (property inherited from BaseCommandController) can be used.</p>
32  *
33  * <p>This command controller should preferrable not be used to handle form
34  * submission, because functionality for forms is more offered in more
35  * detail by the {@link org.springframework.web.portlet.mvc.AbstractFormController
36  * AbstractFormController} and its corresponding implementations.</p>
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 John A. Lewis
46  * @author Juergen Hoeller
47  * @since 2.0
48  * @see #setCommandClass
49  * @see #setCommandName
50  * @see #setValidator
51  */

52 public abstract class AbstractCommandController extends BaseCommandController {
53
54     /**
55      * This render parameter is used to indicate forward to the render phase
56      * that a valid command (and errors) object is in the session.
57      */

58     private static final String JavaDoc COMMAND_IN_SESSION_PARAMETER = "command-in-session";
59
60     private static final String JavaDoc TRUE = Boolean.TRUE.toString();
61
62
63     /**
64      * Create a new AbstractCommandController.
65      */

66     public AbstractCommandController() {
67     }
68
69     /**
70      * Create a new AbstractCommandController.
71      * @param commandClass class of the command bean
72      */

73     public AbstractCommandController(Class JavaDoc commandClass) {
74         setCommandClass(commandClass);
75     }
76
77     /**
78      * Create a new AbstractCommandController.
79      * @param commandClass class of the command bean
80      * @param commandName name of the command bean
81      */

82     public AbstractCommandController(Class JavaDoc commandClass, String JavaDoc commandName) {
83         setCommandClass(commandClass);
84         setCommandName(commandName);
85     }
86
87
88     protected final void handleActionRequestInternal(ActionRequest request, ActionResponse response)
89             throws Exception JavaDoc {
90
91         // Create the command object.
92
Object JavaDoc command = getCommand(request);
93
94         // Compute the errors object.
95
PortletRequestDataBinder binder = bindAndValidate(request, command);
96         BindException errors = new BindException(binder.getBindingResult());
97
98         // Actually handle the action.
99
handleAction(request, response, command, errors);
100
101         // Pass the command and errors forward to the render phase.
102
setRenderCommandAndErrors(request, command, errors);
103         setCommandInSession(response);
104     }
105
106     protected final ModelAndView handleRenderRequestInternal(
107             RenderRequest request, RenderResponse response) throws Exception JavaDoc {
108
109         Object JavaDoc command = null;
110         BindException errors = null;
111
112         // get the command and errors objects from the session, if they exist
113
if (isCommandInSession(request)) {
114             logger.debug("Render phase obtaining command and errors objects from session");
115             command = getRenderCommand(request);
116             errors = getRenderErrors(request);
117         }
118         else {
119             logger.debug("Render phase creating new command and errors objects");
120         }
121
122         // If no command object was in the session, create a new one.
123
if (command == null) {
124             command = getCommand(request);
125         }
126
127         // If no errors object was in the session, compute a new one.
128
if (errors == null) {
129             PortletRequestDataBinder binder = bindAndValidate(request, command);
130             errors = new BindException(binder.getBindingResult());
131         }
132
133         return handleRender(request, response, command, errors);
134     }
135
136
137     /**
138      * Template method for request handling, providing a populated and validated instance
139      * of the command class, and an Errors object containing binding and validation errors.
140      * <p>Call <code>errors.getModel()</code> to populate the ModelAndView model
141      * with the command and the Errors instance, under the specified command name,
142      * as expected by the "spring:bind" tag.
143      * @param request current action request
144      * @param response current action response
145      * @param command the populated command object
146      * @param errors validation errors holder
147      * @see org.springframework.validation.Errors
148      * @see org.springframework.validation.BindException#getModel
149      */

150     protected abstract void handleAction(
151             ActionRequest request, ActionResponse response, Object JavaDoc command, BindException errors)
152             throws Exception JavaDoc;
153
154     /**
155      * Template method for render request handling, providing a populated and validated instance
156      * of the command class, and an Errors object containing binding and validation errors.
157      * <p>Call <code>errors.getModel()</code> to populate the ModelAndView model
158      * with the command and the Errors instance, under the specified command name,
159      * as expected by the "spring:bind" tag.
160      * @param request current render request
161      * @param response current render response
162      * @param command the populated command object
163      * @param errors validation errors holder
164      * @return a ModelAndView to render, or null if handled directly
165      * @see org.springframework.validation.Errors
166      * @see org.springframework.validation.BindException#getModel
167      */

168     protected abstract ModelAndView handleRender(
169             RenderRequest request, RenderResponse response, Object JavaDoc command, BindException errors)
170             throws Exception JavaDoc;
171
172
173     /**
174      * Return the name of the render parameter that indicates there
175      * is a valid command (and errors) object in the session.
176      * @return the name of the render parameter
177      * @see javax.portlet.RenderRequest#getParameter
178      */

179     protected String JavaDoc getCommandInSessionParameterName() {
180         return COMMAND_IN_SESSION_PARAMETER;
181     }
182
183     /**
184      * Set the action response parameter that indicates there is a
185      * command (and errors) object in the session for the render phase.
186      * @param response the current action response
187      * @see #getCommandInSessionParameterName
188      * @see #isCommandInSession
189      */

190     protected final void setCommandInSession(ActionResponse response) {
191         if (logger.isDebugEnabled()) {
192             logger.debug("Setting render parameter [" + getCommandInSessionParameterName() +
193                     "] to indicate a valid command (and errors) object are in the session");
194         }
195         try {
196             response.setRenderParameter(getCommandInSessionParameterName(), TRUE);
197         }
198         catch (IllegalStateException JavaDoc ex) {
199             // Ignore in case sendRedirect was already set.
200
}
201     }
202
203     /**
204      * Determine if there is a valid command (and errors) object in the
205      * session for this render request.
206      * @param request current render request
207      * @return if there is a valid command object in the session
208      * @see #getCommandInSessionParameterName
209      * @see #setCommandInSession
210      */

211     protected final boolean isCommandInSession(RenderRequest request) {
212         return TRUE.equals(request.getParameter(getCommandInSessionParameterName()));
213     }
214
215 }
216
Popular Tags