Code:
/*
* MPSC-Spring - A library of common code to use with the Spring framework
* Copyright (C) 2005 Matt Parker
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package uk.co.mpcontracting.modules.spring.controller;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;
import uk.co.mpcontracting.modules.spring.model.AbstractControlModel;
/**
*
* @author mparker
*/
public abstract class AbstractFormController extends SimpleFormController
{
private static Log log = LogFactory.getLog(AbstractFormController.class);
private static final String DEFAULT_MESSAGE = "error.standard.defaultMessage";
protected abstract Map process(HttpServletRequest request, HttpServletResponse response, Object command,
BindException errors) throws Exception;
protected ModelAndView processFormSubmission(HttpServletRequest request, HttpServletResponse response,
Object command, BindException errors) throws Exception
{
if (errors.hasErrors() || isFormChangeRequest(request))
{
if (log.isDebugEnabled())
{
log.debug("Data binding errors - " + errors.getErrorCount());
}
return (showForm(request, response, errors, getControlModel(request)));
}
else
{
if (log.isDebugEnabled())
{
log.debug("No errors - processing submit");
}
return (onSubmit(request, response, command, errors));
}
}
public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command,
BindException errors) throws Exception
{
Map controlModel = process(request, response, command, errors);
if (errors.hasErrors())
{
return (showForm(request, response, errors, getControlModel(request)));
}
else
{
ModelAndView modelAndView;
String successView = getSuccessView();
if (successView.indexOf('.') > -1)
{
// This is a redirect URL
modelAndView = new ModelAndView(new RedirectView(successView, true));
}
else
{
// This is a view name
modelAndView = new ModelAndView(successView);
if (controlModel != null)
{
modelAndView.addAllObjects(controlModel);
}
}
return (modelAndView);
}
}
void createFormBackingObject(HttpServletRequest request) throws Exception
{
if (log.isDebugEnabled())
{
log.debug("Creating form backing object");
}
// Clear out any existing commands that may still exist in the session
request.getSession().removeAttribute(getFormSessionAttributeName(request));
request.setAttribute(getCommandName(), createCommand());
}
protected Map getControlModel(HttpServletRequest request) throws Exception
{
return (AbstractControlModel.retrieveControlModel(request));
}
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder)
throws Exception
{
binder.registerCustomEditor(Double.class, new CustomNumberEditor(Double.class, true));
binder.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, true));
initCustomBinder(request, binder);
}
protected void initCustomBinder(HttpServletRequest request, ServletRequestDataBinder binder)
{
// Nothing - override in subclass if required
}
protected void addError(BindException errors, String errorMessage, Object[] arguments)
{
if (errorMessage != null)
{
errors.addError(new ObjectError(getCommandName(), new String[] {errorMessage}, arguments, null));
}
else
{
errors.addError(new ObjectError(getCommandName(), new String[] {DEFAULT_MESSAGE}, arguments, null));
}
}
}
Like most people I maintain a library of useful code to extend the Spring framework in the way I'd like it to work.