使用Ajax与SimpleFormCotroller在Spring MVC

问题描述:

我正在开发使用JSP的应用 SimpleFormController中 与Spring MVC 3.0.2使用Hibernate。一切安好。我还使用 验证 ,以验证在服务器端的形式。它也进展顺利。

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.

现在,我需要使用Ajax作为一个例子,当一个国家选择从下拉(<形式:选择><形式:选项>< /形式:选项>&LT ; /格式:选择> ),相当于该国的国家应该填充从状态的数据库下拉

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.

我在做的地方使用Ajax这样的事情,但尚未使用Spring MVC。我已经经历了很多教程/约 SimpleFormController中在谷歌的文章走了,但他们没有被使用Ajax。我找不到有关如何使用Ajax和 SimpleFormController中一个想法。

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.

使用注解控制器( @Controller )的东西可以很容易的方法,因为可以使用映射的 @RequestMapping 注释(不过我还没有使用它,但我想我可以)。

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

但随着 SimpleFormController中,我没有关于如何处理在春节控制器Ajax请求的任何precise想法(这方法来映射以及如何) 。随着 SimpleFormController中,我通常用的onsubmit() showForm()相关 referenceData()的方法。

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.

请您揭露如何能一个Ajax请求对 SimpleFormController中做了一些思考,哪些方法可以映射又如何? (我不希望全code了。一个很简单的例子(当且仅当它有可能的),或者进一步的具体环节,其中使用Ajax与 SimpleFormController中解释将是相当足以让我学习)。

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

在补充DARDO的回答,同时使用弹簧MVC 2和MVC 3控制器是可能的,但有点棘手成立。

In complement to dardo's answer, using both spring MVC 2 and MVC 3 controllers is possible, but a bit tricky to set up.

为了在同一个Spring上下文中使用这两种 SimpleFormController中 @Controller 控制器,我用下面的定义

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>