I'm developing an application in JSP using SimpleFormController
with Spring MVC 3.0.2 using Hibernate. Everything is fine. I'm also using Validator
to validate forms on the server side. It's also going on well.
Now, I need to use Ajax as an example, when a country is selected from a drop down (<form:select><form:option></form:option></form:select>
), the states corresponding to that country should be populated from the database in the state drop down.
I have done such things using Ajax in places but yet not with Spring MVC. I have gone through many tutorials/articles about SimpleFormController
on Google but none of them were using Ajax. I couldn't find a single idea about how to use Ajax with SimpleFormController
.
With annotated controllers (@Controller
), the thing can be made easy because methods can be mapped using the @RequestMapping
annotation (nevertheless I haven't yet used it but I think I can).
But with SimpleFormController
, I don't have any precise idea about how to handle Ajax requests in the Spring controller (which methods to be mapped and how). With SimpleFormController
, I'm usually associated with the onSubmit()
, showForm()
and referenceData()
methods.
Could you please expose some thoughts on how can an Ajax request be made on SimpleFormController
, which methods can be mapped and how? (I don't want the full code anymore. A very simple example (if and only if it's possible) or furthermore specific links where the use of Ajax with the SimpleFormController
is explained would be quite enough for me to study).
In complement to dardo's answer, using both spring MVC 2 and MVC 3 controllers is possible, but a bit tricky to set up.
In order to use both
SimpleFormController
and@Controller
controllers in the same Spring context, I used to following definition :You could always just have a separate @Controller to handle the ajax requests. If you can have custom jsp on the view, there is nothing stopping you from handling an ajax request on the page. Just bind the onchange event of the select box to an ajax call pointing to the other Controller you made.
In terms of keeping it bound to just that SimpleFormController, I don't think this is possible, but if you create a new RESTful controller that the form would use, other parts of the website will be able to use this new controller as well.