Announcement Announcement Module
Collapse
No announcement yet.
adding a model to the success view Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • adding a model to the success view

    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

  • #2
    the only way I can see is by overriding the onSubmit method
    You can override doSubmitAction

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

    Comment


    • #3
      You can override doSubmitAction
      Not really, there is no way to change or add anything to the model from doSubmitAction.

      Comment


      • #4
        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

        Comment


        • #5
          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.

          Comment


          • #6
            I have an AbstractFormController that deals with this problem like so:-

            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.

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

            Bob

            Comment


            • #8
              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

              Comment


              • #9
                thanks. will look at this.

                Comment

                Working...
                X