Reputation: 72
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...
Upvotes: 0
Views: 198
Reputation: 8187
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.
Upvotes: 1