KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > mvc > throwaway > ThrowawayControllerHandlerAdapter


1 /*
2  * Copyright 2002-2005 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.throwaway;
18
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21
22 import org.springframework.web.bind.ServletRequestDataBinder;
23 import org.springframework.web.servlet.HandlerAdapter;
24 import org.springframework.web.servlet.ModelAndView;
25
26 /**
27  * Adapter to use the ThrowawayController workflow interface with the
28  * generic DispatcherServlet. Does not support last-modified checks.
29  *
30  * <p>This is an SPI class, not used directly by application code.
31  * It can be explicitly configured in a DispatcherServlet context, to use a
32  * customized version instead of the default ThrowawayControllerHandlerAdapter.
33  *
34  * @author Juergen Hoeller
35  * @since 08.12.2003
36  */

37 public class ThrowawayControllerHandlerAdapter implements HandlerAdapter {
38
39     public static final String JavaDoc DEFAULT_COMMAND_NAME = "throwawayController";
40
41     private String JavaDoc commandName = DEFAULT_COMMAND_NAME;
42
43
44     /**
45      * Set the name of the command in the model.
46      * The command object will be included in the model under this name.
47      */

48     public final void setCommandName(String JavaDoc commandName) {
49         this.commandName = commandName;
50     }
51
52     /**
53      * Return the name of the command in the model.
54      */

55     public final String JavaDoc getCommandName() {
56         return this.commandName;
57     }
58
59
60     public boolean supports(Object JavaDoc handler) {
61         return (handler instanceof ThrowawayController);
62     }
63
64
65     /**
66      * This implementation binds request parameters to the ThrowawayController
67      * instance and then calls <code>execute</code> on it.
68      * @see #createBinder
69      * @see ThrowawayController#execute
70      */

71     public ModelAndView handle(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Object JavaDoc handler)
72             throws Exception JavaDoc {
73
74         ThrowawayController throwaway = (ThrowawayController) handler;
75
76         ServletRequestDataBinder binder = createBinder(request, throwaway);
77         binder.bind(request);
78         binder.closeNoCatch();
79
80         return throwaway.execute();
81     }
82
83     /**
84      * Create a new binder instance for the given command and request.
85      * <p>Called by <code>bindAndValidate</code>. Can be overridden to plug in
86      * custom ServletRequestDataBinder subclasses.
87      * <p>Default implementation creates a standard ServletRequestDataBinder,
88      * sets the specified MessageCodesResolver (if any), and invokes initBinder.
89      * Note that <code>initBinder</code> will not be invoked if you override this method!
90      * @param request current HTTP request
91      * @param command the command to bind onto
92      * @return the new binder instance
93      * @throws Exception in case of invalid state or arguments
94      * @see #initBinder
95      * @see #getCommandName
96      */

97     protected ServletRequestDataBinder createBinder(HttpServletRequest JavaDoc request, ThrowawayController command)
98         throws Exception JavaDoc {
99
100         ServletRequestDataBinder binder = new ServletRequestDataBinder(command, getCommandName());
101         initBinder(request, binder);
102         return binder;
103     }
104
105     /**
106      * Initialize the given binder instance, for example with custom editors.
107      * Called by <code>createBinder</code>.
108      * <p>This method allows you to register custom editors for certain fields of your
109      * command class. For instance, you will be able to transform Date objects into a
110      * String pattern and back, in order to allow your JavaBeans to have Date properties
111      * and still be able to set and display them in an HTML interface.
112      * <p>Default implementation is empty.
113      * @param request current HTTP request
114      * @param binder new binder instance
115      * @throws Exception in case of invalid state or arguments
116      * @see #createBinder
117      * @see org.springframework.validation.DataBinder#registerCustomEditor
118      * @see org.springframework.beans.propertyeditors.CustomDateEditor
119      */

120     protected void initBinder(HttpServletRequest JavaDoc request, ServletRequestDataBinder binder)
121         throws Exception JavaDoc {
122     }
123
124
125     /**
126      * This implementation always returns -1, as last-modified checks are not supported.
127      */

128     public long getLastModified(HttpServletRequest JavaDoc request, Object JavaDoc handler) {
129         return -1;
130     }
131
132 }
133
Popular Tags