java Spring - 用一个控制器处理多个表单提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1838814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring - handle multiple forms submit with one controller
提问by robinmag
Is it possible to have one controller to handle multiple forms in spring?
是否可以让一个控制器在 spring 中处理多种表单?
For example, i have 3 steps registration that map with "/register" url. Is it possible to have only one RegisterController that can handle all registration's steps form submit?
例如,我有 3 个步骤注册,映射到“/register” url。是否有可能只有一个 RegisterController 可以处理表单提交的所有注册步骤?
回答by skaffman
That depends on the style of controllers you're using. If you use Spring 2.0-style form controllers (e.g. a subclass of SimpleFormControlleror AbstractFormController), then you might have some difficulty. If, however, you're using Spring 2.5-style annotated @Controllers, then these are very flexible, and you should be able to handle pretty much any cmplexity you desire in one controller class.
这取决于您使用的控制器的风格。如果您使用 Spring 2.0 样式的表单控制器(例如SimpleFormController或的子类AbstractFormController),那么您可能会遇到一些困难。但是,如果您使用的是 Spring 2.5 样式的带注释的@Controllers,那么它们非常灵活,并且您应该能够在一个控制器类中处理几乎任何您想要的复杂性。
回答by Mike
Like skaffmansaid you can use @Controller that are very flexible. But if you're writting a Wizard, you should take a look at AbstractWizardFormController. It handles all steps and validation of a wizard. It also keeps the same backing object for each form submission. When your wizard is on the final action, you can take this object and update it at the database.
就像skaffman说的,你可以使用非常灵活的 @Controller。但是如果你正在编写一个向导,你应该看看 AbstractWizardFormController。它处理向导的所有步骤和验证。它还为每个表单提交保留相同的支持对象。当您的向导执行最终操作时,您可以获取此对象并在数据库中更新它。
You can find an exemple at : http://www.cs.bham.ac.uk/~aps/syllabi/2004_2005/issws/h04/spring.html#AbstractWizardFormController
您可以在以下位置找到示例:http: //www.cs.bham.ac.uk/~aps/syllabi/2004_2005/issws/h04/spring.html#AbstractWizardFormController
回答by Mike
you can use multi action controller .In Spring Single Controller Handel Multipal Method by method name resolver
您可以使用多动作控制器。在 Spring Single Controller Handel Multipal Method 中按方法名称解析器
<bean id="id" class="controller class"> <property name="name"><ref bean="service Name"/></property > <property
name="methodNameResolver"><ref bean="name of Resolver"/></property>
</bean>
------------------------------------------------------------------------ <bean id="name of Resolver"
class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings"> <props> <prop
key="/url.com">MethodName</prop> </props> </property> </bean> This can
be Help you use single controller
------------------------------------------------------------------------
回答by haju
QuestionFor example, i have 3 steps registration that map with "/register" url. Is it possible to have only one RegisterController that can handle all registration's steps form submit?
问题例如,我有 3 个步骤注册,映射到“/register” url。是否有可能只有一个 RegisterController 可以处理表单提交的所有注册步骤?
Proposed Solution:Spring 3 - Spring-mvc controller using servlet mapping to your controller. Using the mapping in your url request you will always go to the same controller.
建议的解决方案:Spring 3 - Spring-mvc 控制器使用 servlet 映射到您的控制器。使用您的 url 请求中的映射,您将始终转到同一个控制器。
You can create a servlet & servlet mapping that maps to your controller through the spring DispatcherServlet.
您可以创建一个 servlet 和 servlet 映射,通过 spring DispatcherServlet 映射到您的控制器。
Create a servlet & servlet mapping to handle your requests.
创建一个 servlet 和 servlet 映射来处理您的请求。
Web.XML
网页.XML
<servlet>
<description>
</description>
<display-name>TestServ</display-name>
<servlet-name>main</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test/*</url-pattern>
</servlet-mapping>
Create a spring config file associated to the servlet.
创建与 servlet 关联的 spring 配置文件。
In this case since the name of my servlet is test, it will be test-servlet.xml file that goes into your WEB-INF folder at the root.
在这种情况下,由于我的 servlet 的名称是 test,它将是 test-servlet.xml 文件,该文件进入根目录下的 WEB-INF 文件夹中。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean name="/registration/*" class="register.ResgisterController"/>
Create your controller class to map your requests.
创建您的控制器类来映射您的请求。
package registration;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import registration.User;
@Controller
public final class RegistrationController {
public RosterController() {
}
//Spring lets you You can access your spring mvc model from the controller automatically
@RequestMapping
public void list(Model model) {
//do something
}
//Extract a registration id from the request and you can use in your model
@RequestMapping
public void member(@RequestParam("registrationId") Integer id, Model model) {
//do something
}
}
回答by Rakesh Juyal
you will have to use 'MultiActionController', but there are some complication in validation porting if you will use this controller.
Solution of similar problem is given @ http://www.scribd.com/doc/20156448/Multi-Action-Controller-With-Validation
您将不得不使用“ MultiActionController”,但如果您将使用此控制器,则验证移植会有些复杂。
给出了类似问题的解决方案@ http://www.scribd.com/doc/20156448/Multi-Action-Controller-With-Validation
回答by bassem
you can use Spring webFlow and annotate your controller as @controller
您可以使用 Spring webFlow 并将您的控制器注释为 @controller

