Announcement Announcement Module
Collapse
No announcement yet.
SimpleFormContoller - return ModelAndView for both isFormSubmission cases Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    I don't quite understand the question but, would

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

    Comment


    • #3
      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, 05:09 PM.

      Comment


      • #4
        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, 05:16 PM.

        Comment

        Working...
        X