001: /*
002: * Copyright 2002-2005 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.throwaway;
018:
019: import javax.servlet.http.HttpServletRequest;
020: import javax.servlet.http.HttpServletResponse;
021:
022: import org.springframework.web.bind.ServletRequestDataBinder;
023: import org.springframework.web.servlet.HandlerAdapter;
024: import org.springframework.web.servlet.ModelAndView;
025:
026: /**
027: * Adapter to use the ThrowawayController workflow interface with the
028: * generic DispatcherServlet. Does not support last-modified checks.
029: *
030: * <p>This is an SPI class, not used directly by application code.
031: * It can be explicitly configured in a DispatcherServlet context, to use a
032: * customized version instead of the default ThrowawayControllerHandlerAdapter.
033: *
034: * @author Juergen Hoeller
035: * @since 08.12.2003
036: */
037: public class ThrowawayControllerHandlerAdapter implements
038: HandlerAdapter {
039:
040: public static final String DEFAULT_COMMAND_NAME = "throwawayController";
041:
042: private String commandName = DEFAULT_COMMAND_NAME;
043:
044: /**
045: * Set the name of the command in the model.
046: * The command object will be included in the model under this name.
047: */
048: public final void setCommandName(String commandName) {
049: this .commandName = commandName;
050: }
051:
052: /**
053: * Return the name of the command in the model.
054: */
055: public final String getCommandName() {
056: return this .commandName;
057: }
058:
059: public boolean supports(Object handler) {
060: return (handler instanceof ThrowawayController);
061: }
062:
063: /**
064: * This implementation binds request parameters to the ThrowawayController
065: * instance and then calls <code>execute</code> on it.
066: * @see #createBinder
067: * @see ThrowawayController#execute
068: */
069: public ModelAndView handle(HttpServletRequest request,
070: HttpServletResponse response, Object handler)
071: throws Exception {
072:
073: ThrowawayController throwaway = (ThrowawayController) handler;
074:
075: ServletRequestDataBinder binder = createBinder(request,
076: throwaway);
077: binder.bind(request);
078: binder.closeNoCatch();
079:
080: return throwaway.execute();
081: }
082:
083: /**
084: * Create a new binder instance for the given command and request.
085: * <p>Called by <code>bindAndValidate</code>. Can be overridden to plug in
086: * custom ServletRequestDataBinder subclasses.
087: * <p>Default implementation creates a standard ServletRequestDataBinder,
088: * sets the specified MessageCodesResolver (if any), and invokes initBinder.
089: * Note that <code>initBinder</code> will not be invoked if you override this method!
090: * @param request current HTTP request
091: * @param command the command to bind onto
092: * @return the new binder instance
093: * @throws Exception in case of invalid state or arguments
094: * @see #initBinder
095: * @see #getCommandName
096: */
097: protected ServletRequestDataBinder createBinder(
098: HttpServletRequest request, ThrowawayController command)
099: throws Exception {
100:
101: ServletRequestDataBinder binder = new ServletRequestDataBinder(
102: command, getCommandName());
103: initBinder(request, binder);
104: return binder;
105: }
106:
107: /**
108: * Initialize the given binder instance, for example with custom editors.
109: * Called by <code>createBinder</code>.
110: * <p>This method allows you to register custom editors for certain fields of your
111: * command class. For instance, you will be able to transform Date objects into a
112: * String pattern and back, in order to allow your JavaBeans to have Date properties
113: * and still be able to set and display them in an HTML interface.
114: * <p>Default implementation is empty.
115: * @param request current HTTP request
116: * @param binder new binder instance
117: * @throws Exception in case of invalid state or arguments
118: * @see #createBinder
119: * @see org.springframework.validation.DataBinder#registerCustomEditor
120: * @see org.springframework.beans.propertyeditors.CustomDateEditor
121: */
122: protected void initBinder(HttpServletRequest request,
123: ServletRequestDataBinder binder) throws Exception {
124: }
125:
126: /**
127: * This implementation always returns -1, as last-modified checks are not supported.
128: */
129: public long getLastModified(HttpServletRequest request,
130: Object handler) {
131: return -1;
132: }
133:
134: }
|