English 中文(简体)
Spring Advice - submitting a form
原标题:

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?

问题回答

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.

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.

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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签