views:

535

answers:

3

Hi,

I am having serious problems with code I have written with Spring so I have decided to start from scratch and ask for advice. Here are my requirements:

  1. When the page first loads I need a list of objects retrieved from the DB that I can access on the JSP.
  2. I use this list to populate a drop down.
  3. When the user selects an object from the drop down the form below is populated by the appropriate data (all of this data is available as it is retrieved when the page is first
    loaded)
  4. The user can modify this data and submit the form. I need to save this data to the DB
  5. The page should be reloaded and needs to retrieve the list of objects from the DB again as they have changed and make this list
    available to the JSP.

I have been using SimpleFormController and the referenceData() and onSubmitAction() methods but I'm not sure if this is the best solution. I think my problem is that after onSubmitAction is finished the list of objects is not available in the JSP as referenceData() is not called after onSubmitAction() finishes.

Apologies if this is a silly request. I have been googling and looking for tutorials for 2 days and I cannot find an example that does what I need it to do.

So my question is which methods should I be implementing to meet these requirements?

A: 

The old Spring way of using the Controller class hierarchy has lots of drawbacks compared to the newer, more flexible way of using annotation based controllers.

That said, have you tried to send a redirect after processing the form submission? This both solves the problem of the user being prompted if it's ok to resubmit the form if he reloads the page.

fforw
Thanks for the fast reply. Is there a particular method I need to call to do this, if you have an example of the line of code that would be great to save me more hours of doing things wrong!
Caroline
I believe fforw is referring to the (annotation based configuration for Spring MVC)[http://static.springsource.org/spring/docs/2.5.x/reference/mvc.html#mvc-annotation] and/or the REST like controller you can create with this (especially in the upcoming Spring 3.0).
Chris Harcourt
A: 

The confusing thing to me about your question is the onSubmitAction() call -- are you using the Spring portlet code or the servlet code?

If you're using the portlet code, then you should be OK also overriding onSubmitRender() to return a RedirectView redirecting the user back to the same page. As you say, if you use the same page as your default success view, you don't go back through the referenceData() call; if you instead redirect your user to the page, the user is taken through the entire page-load process which includes the referenceData() call. So you just have to include an overridden onSubmitRender() which returns something like this:

return new ModelAndView(new RedirectView(url, true));

If you're using the servlet code, there's no onSubmitAction(), only onSubmit() -- and at the end of your overridden onSubmit(), you'd do the same as above, returning a new RedirectView() to your same page.

delfuego
Thanks for the tips. I tried this and it works to a certain degree. I get no exception thrown which I was getting previously but the referenceData() method is not being called still. I am setting the URL to be the JSP. Should this not call the referenceData() method when the jsp loads or is there some other config that I need to do?
Caroline
Don't set the URL to the JSP URL, set it to the Action URL (the mapping that ties your SimpleFormController to the URL).In other words, if you have a mapping for "/showForm.do" to your SimpleFormController, then your URL should be "/showForm.do".
delfuego
A: 

This can be solved in a few different ways:

  1. Set your successView to something that will forward to the form command as if it were an initial request (e.g. set a parameter or something that indicates the form shouldn't be resubmitted)
  2. Set your successView to a redirect back to the original form. (e.g. redirect:/some/url)
  3. Have your form submit logic call the referenceData method to populate the request attributes you need.

1 and 2 have the advantage of reusing your existing form display logic. In addition, 2 has the advantage of avoiding a submit if the user reloads the page after submitting the form once.

David
My scccessview is set to the name of the JSP <property name="successView" value="communications/communicationsAdmin"/>. I'm not sure where else I can send it. Do you happen to have an example, both options sound like they would work but I dont know how to implement them. I dont really want to go with option 3 if at all possible.
Caroline
Updated #2...if you use a view name of `redirect:/some/url`, Spring MVC will issue a redirect to `/some/url`
David