Source code for /src/java/com/coolshare/springapp/web/ReflectFormController.java

// =====================================================================//
// Author: Mark Qian <markqian@hotmail.com>                             //
// WWW: http://www.coolshare.com/                                       //
// Copyright (c) 2006, Mark Qian                                        //
//                                                                      //
// You must contact Mark Qian to get a permission of use                //
// in case you want to make any use of the codes except viewing it     //
// on Mark's site.                                                      //
//======================================================================//
package com.coolshare.springapp.web;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.coolshare.springapp.bus.Bamboo;
import com.coolshare.springapp.bus.BambooManager;
import com.coolshare.springapp.bus.Source;

public abstract class ReflectFormController extends SimpleFormController {

	/** Logger for this class and subclasses */
	protected final Log logger = LogFactory.getLog(getClass());

	/*
	 * (non-Javadoc) Original onBind is marked as final to prevent custom
	 * binding being overriden.
	 * 
	 * @see org.springframework.web.servlet.mvc.BaseCommandController#onBind(javax.servlet.http.HttpServletRequest,
	 *      java.lang.Object, org.springframework.validation.BindException)
	 */
	protected final void onBind(HttpServletRequest request, Object command,
			BindException errors) throws Exception {

		ReflectDataBinder binder = new ReflectDataBinder();
		Map beanMap = binder.bind(request);
		((BeanMapCommand) command).setBeanMap(beanMap);
		onBind(request, command, errors, beanMap);
	}

	// Customized onBind
	protected void onBind(HttpServletRequest request, Object command,
			BindException errors, Map beanMap) throws Exception {

	}

	/*
	 * (non-Javadoc) Original onSubmit is marked as final to enfore subclass to
	 * use the new onSubmit below
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(java.lang.Object)
	 */
	public final ModelAndView onSubmit(Object command) throws ServletException {
		BeanMapCommand form = (BeanMapCommand) command;
		Map beanMap = ((BeanMapCommand) command).getBeanMap();

		return onSubmit(beanMap);
	}

	/**
	 * The new onSubmit with beanMap instead of command as parameter.
	 * 
	 * @param beanMap -
	 *            The ready-to-use beans involved in the incoming HttpRequest is
	 *            available in the beanMap
	 * @return
	 * @throws ServletException
	 */
	public ModelAndView onSubmit(Map beanMap) throws ServletException {
		return new ModelAndView(new RedirectView(getSuccessView()));
	}
}