Results 1 to 4 of 4

Thread: SimpleFormContoller - return ModelAndView for both isFormSubmission cases

  1. #1
    Join Date
    Nov 2011
    Location
    Bavaria, Germany
    Posts
    3

    Default SimpleFormContoller - return ModelAndView for both isFormSubmission cases

    Hi!

    I'm working on a SFC that should return a ModelAndView for both the FormView and the SuccessView.

    While for the SuccessView it was easy to do

    Code:
    	protected ModelAndView onSubmit(Object command, BindException errors) throws Exception {
    	...
    
    	return new ModelAndView(this.getSuccessView(), "guestbook", model);	}
    but i cannot find a way to do it for the FormView. It is not enough to simply return the View, i also need to specify a model, so i cannot just work with a command and backingForm thingies.

    I don't want to implement the Controller Interface myself, as the SFC is - except for this case - sufficient. I hope someone has a hint, i'm working for hours on this problem now with almost no progress.

    Thanks in advance.

  2. #2

    Default

    I don't quite understand the question but, would

    Code:
    return new ModelAndView(this.getFormView(), "guestbook", model);
    not do the trick?

  3. #3
    Join Date
    Nov 2011
    Location
    Bavaria, Germany
    Posts
    3

    Default

    onSubmit() is called, when the user views an actual formView and hits a submit button. So when the controller renders the successView, it can access the model the onSubmit method returns (together with a successView).

    However, how to supply the formView with a model?

    I found a method showForm() in AbstractController (which is the parent class of SFC) that might be useful, but i didn't get it to work [SimpleFormController, the guestbook bean gets injected:]

    Code:
    @Override
    protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors, Map controlModel) throws Exception {
    
    	ModelAndView mav = super.showForm(request, response, errors, controlModel);
    	mav.getModelMap().put("guestbook", guestbook);
    		
    	return mav;
    }
    "guestbook" is not accessible via tags in the JSP. I think i didn't understand the controlModel Map correctly, although in the tag i found out that although i cannot access "guestbook" it doesn't seem to be null either.

    Confusing.
    Last edited by weiglt; Nov 21st, 2011 at 04:09 PM.

  4. #4
    Join Date
    Nov 2011
    Location
    Bavaria, Germany
    Posts
    3

    Default

    The Solution: (boy it is so easy if you know it!)
    Ya, the solution was to override the showForm() method. But in a very simple way:

    Code:
    protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors, Map controlModel) throws Exception {
    
    	ModelAndView mav = super.showForm(request, response, errors, controlModel);
    	mav.getModelMap().put("guestbook", guestbook);
    
    	return mav;
    }
    This way of course both Views of the SimpleFormController can have an injected model bean, which can be accessed by via tags.

    And because google results showed me that other people tried to use the SimpleFormController in a similar way:

    If you want to redirect to the same Controller again (like to show his own recently made updates), use in the xml

    Code:
    <bean name="/guestbook.app" class="de.tum.in.dss.GuestbookController">
    	<property name="formView" value="GuestbookSite" />
    	<property name="successView" value="redirect:/guestbook.app" />
    Also, the code in my first post was a bad way to call onSubmit! Do not do this, because it destroys the controler logic. Use super and return the manipulated values instead. Sorry, i'm new to spring (haha, everyone always says that).
    Last edited by weiglt; Nov 21st, 2011 at 04:16 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •