1

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).

Tiny
  • 27,221
  • 105
  • 339
  • 599
  • 1
    The spring form controller is designed for use with html/jsp not with ajax. I suggest you read up on spring and ajax: http://blog.springsource.org/2010/01/25/ajax-simplifications-in-spring-3-0/ – G-Man Aug 02 '12 at 13:22

2 Answers2

6

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.

dardo
  • 4,870
  • 4
  • 35
  • 52
  • Tried to use a separate annotated controller along with the `SimpleFormController` but found it wasn't possible. It caused an exception for all other `SimpleFormController` controllers `javax.servlet.ServletException: No adapter for handler [controller.CountryController@1a91b2b]: Does your handler implement a supported interface like Controller?` The container expects all the controllers to be annotated controllers, if an attempt is made to do so. I feel sorry to say. – Tiny Aug 02 '12 at 18:53
  • Might want to take a look at your context a bit, it shouldn't do this unless you're overriding what Spring does automatically: http://forum.springsource.org/showthread.php?52432-problem-with-controller-annotation-No-adapter-for-handler/page2 – dardo Aug 02 '12 at 18:55
  • In my `web.xml` file, `/WEB-INF/applicationContext.xml` was not defaulted tried to add within `` as mentioned in that link but it made no difference. Otherwise, everything matches your preceding link. Is it not possible to use Ajax within a `SimpleFormController` at all? – Tiny Aug 02 '12 at 19:21
2

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 :

<!-- ======== MVC Spring 3.x ======== -->

<!-- Scans within the base package of the application for @Components to configure as beans @Controller, @Service, @Configuration, etc. -->
<context:component-scan base-package="com.your.package" />

<!-- Enables the Spring MVC @Controller programming model -->
<!-- It's a shortcut equivalent to the (more complete) bean definition below (see bean AnnotationMethodHandlerAdapter).-->
<!--<mvc:annotation-driven />-->

<!-- This HandlerAdapter will register spring 3.x controllers (@Controller) into the DispatcherServlet -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <array>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="writeAcceptCharset" value="false"/>
            </bean>
            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
        </array>
    </property>
</bean>

<!-- This HandlerMapping allows to map urls defined in @RequestMapping annotations to the corresponding controllers -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
</bean>

<!-- ======== MVC Spring 2.x ======== -->

<!-- This HandlerAdapter will register spring 2.x controllers (@Controller) into the DispatcherServlet -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

<!-- Url mapper -->
<bean id="urlMapper" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="urlMap">
        <map>
            <entry key="/foo.do" value-ref="fooController" />
            <entry key="/bar.do" value-ref="barController" />

            ...
        </map>
    </property>
</bean>
Tiny
  • 27,221
  • 105
  • 339
  • 599
Siggen
  • 2,147
  • 1
  • 19
  • 19
  • I was just confusing which answer to offer the bounty. I offered it to the first answer as it made me think so. Don't mind at all, if I did something wrong. By the way, your answer appears to be helpful but I haven't yet tried it, since I'm upgrading my application to include the latest annotated controllers and removing the old-fashioned `SimpleFormController`. I will use this content as a future reference, if such a need arises. Thank you. – Tiny Aug 11 '12 at 18:46