PDA

View Full Version : adding a model to the success view


mariuss
Aug 30th, 2005, 05:29 PM
Hi,

What is the proper way to add a model to the success view of a SimpleFormController?

In most cases a success view will need some model data.

The only way I can see is by overriding the onSubmit method and then returning your own ModelAndView.

I am using Spring 1.1.5

Thanks,
Marius

katentim
Aug 30th, 2005, 06:58 PM
the only way I can see is by overriding the onSubmit method
You can override doSubmitAction (file:///D:/java/spring-framework-1.2.1/docs/api/org/springframework/web/servlet/mvc/SimpleFormController.html#doSubmitAction(java.lang .Object))

and then returning your own ModelAndView
That's pretty much it.

mariuss
Aug 31st, 2005, 01:25 PM
You can override doSubmitAction

Not really, there is no way to change or add anything to the model from doSubmitAction.

simtin
Aug 31st, 2005, 05:46 PM
Hi,

why not easily override onSubmit() and call super.onSubmit() in the end?

-- edit
Aehm ... I think referenceData() is exactly what you're looking for.

Simon

mariuss
Aug 31st, 2005, 05:59 PM
I think referenceData() is exactly what you're looking for.

referenceData() gets called only by showForm(), in case of a successful submit it is not called.

Cowboy Bob
Sep 1st, 2005, 05:31 AM
I have an AbstractFormController that deals with this problem like so:-

/*
* 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.CustomNu mberEditor;
import org.springframework.context.support.MessageSourceA ccessor;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.ServletRequestDataBin der;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormCont roller;
import org.springframework.web.servlet.view.RedirectView;

import uk.co.mpcontracting.modules.spring.model.AbstractC ontrolModel;

/**
*
* @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(getFormSessio nAttributeName(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.

If anyone wants a copy of the whole library (it's LGPL licensed) then post your e-mail here.

Bob

cyber
Sep 1st, 2005, 06:59 AM
cyber@pinoyportal.com

Cowboy Bob
Sep 1st, 2005, 07:25 AM
Cyber...

It appears that you're e-mail won't accept large attachments so here's the e-mail I was going to send and a link to download the file (I don't have huge amounts of bandwidth so be patient with the download speed).

I've just zipped up the current version of my Modules directory. It's not
actually been released "officially" yet so some areas (especially the
security stuff) are not finished - and there are also spurious CVS
directories around which you may want to filter out. But the Spring stuff is
fairly complete.

Look in the "release" directories under each module for the compiled jar
files. The "lib" directories contain the dependency jars.

If you want to build it yourself you'll need to fulfil the criteria in the
various build.properties files in the "ant" directories (or comment out the
<get> tags in the build.xml files).

There's no documentation as yet, so you're on your own there.

Hope there's some useful stuff in there.

http://www.mpcontracting.co.uk/Modules.zip

Oh yeah, I make no warranties on the usefulness of this stuff, or that it's bug free. Use at your own risk. Yada yada

cyber
Sep 1st, 2005, 10:28 PM
thanks. will look at this.