0

I'm running into an issue with SpringMVC, and I would like to know if others have also.

I have a <form:select> binded to a bean property. In the jsp, after the user entered some inputs, the select is populated with <options> via javascript. When I post the form, I try to validate it server-side, and in case of errors, the same jsp is displayed with the <form:errors> fields showing the different errors to be fixed.

The problem I have is that when displaying back the jsp, the value that was previously choosen in the <form:select> is lost.

This must be related to the fact that there are no <form:options> when the page is generated, as they are loaded via js.

What are the solutions to handle this particular case? Must I absolutely load the select <options> server side ?

I haven't found much on this issue on the internet, and I can't believe I am the only one running into it...

1
  • The base data should be present if you want to show the data selected, as on page load its null, So spring cannot comapre and select against null data, So the way ahead is preload your select box (have the data ready) when you call your View in controller. If the ajax is only way, you have to code to retain the old data and set the right value in onsucess handler. Commented Apr 29, 2015 at 6:31

1 Answer 1

1

What the actual problem is,

You Say that the <form:options> values are loaded using javascript and once you post the form. It will create a page post which in-turn creates an HttpRequest to the server.

Incase of errors you redirect back to the same jsp page and it wont contain any entered values

Workaround

You instead post the page using Ajax so that the value will be retained in the form , incase of errors.

Instead making the whole things changing in the server-side. And the alternate way will be loading the options from the requestAttribute , which can be set using model.addAttribute() from your controller.

Sign up to request clarification or add additional context in comments.

1 Comment

We are currently testing the first solution you suggested : submitting the post using Ajax. I will come back to update, but it seems to be the best solution indeed. Thanks for your input!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.