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

  • mariuss
    started a topic adding a model to the success view

    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

  • cyber
    replied
    thanks. will look at this.

    Leave a comment:


  • Cowboy Bob
    replied
    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

    Leave a comment:


  • cyber
    replied
    [email protected]

    Leave a comment:


  • Cowboy Bob
    replied
    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 &#40;C&#41; 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 &#40;at your option&#41; 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
    &#123;
        private static Log log = LogFactory.getLog&#40;AbstractFormController.class&#41;;
        
        private static final String DEFAULT_MESSAGE = "error.standard.defaultMessage";
    
        protected abstract Map process&#40;HttpServletRequest request, HttpServletResponse response, Object command,
            BindException errors&#41; throws Exception;
        
        protected ModelAndView processFormSubmission&#40;HttpServletRequest request, HttpServletResponse response, 
            Object command, BindException errors&#41; throws Exception
        &#123;
            if &#40;errors.hasErrors&#40;&#41; || isFormChangeRequest&#40;request&#41;&#41;
            &#123;
                if &#40;log.isDebugEnabled&#40;&#41;&#41;
                &#123;
                    log.debug&#40;"Data binding errors - " + errors.getErrorCount&#40;&#41;&#41;;
                &#125;
                
                return &#40;showForm&#40;request, response, errors, getControlModel&#40;request&#41;&#41;&#41;;
            &#125;
            else
            &#123;
                if &#40;log.isDebugEnabled&#40;&#41;&#41;
                &#123;
                    log.debug&#40;"No errors - processing submit"&#41;;
                &#125;
                
                return &#40;onSubmit&#40;request, response, command, errors&#41;&#41;;
            &#125;
        &#125;
        
        public ModelAndView onSubmit&#40;HttpServletRequest request, HttpServletResponse response, Object command, 
            BindException errors&#41; throws Exception 
        &#123;
            Map controlModel = process&#40;request, response, command, errors&#41;;
            
            if &#40;errors.hasErrors&#40;&#41;&#41;
            &#123;
                return &#40;showForm&#40;request, response, errors, getControlModel&#40;request&#41;&#41;&#41;;
            &#125;
            else
            &#123;
                ModelAndView modelAndView;
                String successView = getSuccessView&#40;&#41;;
                
                if &#40;successView.indexOf&#40;'.'&#41; > -1&#41;
                &#123;
                    // This is a redirect URL
                    modelAndView = new ModelAndView&#40;new RedirectView&#40;successView, true&#41;&#41;;
                &#125;
                else
                &#123;
                    // This is a view name
                    modelAndView = new ModelAndView&#40;successView&#41;;
                    
                    if &#40;controlModel != null&#41;
                    &#123;
                        modelAndView.addAllObjects&#40;controlModel&#41;;
                    &#125;
                &#125;
    
                return &#40;modelAndView&#41;;
            &#125;
        &#125;
        
        void createFormBackingObject&#40;HttpServletRequest request&#41; throws Exception
        &#123;
            if &#40;log.isDebugEnabled&#40;&#41;&#41;
            &#123;
                log.debug&#40;"Creating form backing object"&#41;;
            &#125;
            
            // Clear out any existing commands that may still exist in the session
            request.getSession&#40;&#41;.removeAttribute&#40;getFormSessionAttributeName&#40;request&#41;&#41;;
            
            request.setAttribute&#40;getCommandName&#40;&#41;, createCommand&#40;&#41;&#41;;
        &#125;
    
        protected Map getControlModel&#40;HttpServletRequest request&#41; throws Exception
        &#123;
            return &#40;AbstractControlModel.retrieveControlModel&#40;request&#41;&#41;;
        &#125;
        
        protected void initBinder&#40;HttpServletRequest request, ServletRequestDataBinder binder&#41; 
            throws Exception
        &#123;
            binder.registerCustomEditor&#40;Double.class, new CustomNumberEditor&#40;Double.class, true&#41;&#41;;
            binder.registerCustomEditor&#40;Integer.class, new CustomNumberEditor&#40;Integer.class, true&#41;&#41;;
            
            initCustomBinder&#40;request, binder&#41;;
        &#125;
        
        protected void initCustomBinder&#40;HttpServletRequest request, ServletRequestDataBinder binder&#41;
        &#123;
            // Nothing - override in subclass if required
        &#125;
    
        protected void addError&#40;BindException errors, String errorMessage, Object&#91;&#93; arguments&#41;
        &#123;
            if &#40;errorMessage != null&#41;
            &#123;
                errors.addError&#40;new ObjectError&#40;getCommandName&#40;&#41;, new String&#91;&#93; &#123;errorMessage&#125;, arguments, null&#41;&#41;;
            &#125;
            else
            &#123;
                errors.addError&#40;new ObjectError&#40;getCommandName&#40;&#41;, new String&#91;&#93; &#123;DEFAULT_MESSAGE&#125;, arguments, null&#41;&#41;;
            &#125;
        &#125;
    &#125;
    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

    Leave a comment:


  • mariuss
    replied
    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.

    Leave a comment:


  • simtin
    replied
    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

    Leave a comment:


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

    Leave a comment:


  • katentim
    replied
    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.

    Leave a comment:

Working...
X