2

Is it possible to have one controller to handle multiple forms in 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?

7 Answers 7

3

That depends on the style of controllers you're using. If you use Spring 2.0-style form controllers (e.g. a subclass of SimpleFormController or 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.

Sign up to request clarification or add additional context in comments.

Comments

3

Like skaffman said 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.

You can find an exemple at : http://www.cs.bham.ac.uk/~aps/syllabi/2004_2005/issws/h04/spring.html#AbstractWizardFormController

1 Comment

I read the newest spring doc and I didn't see that. the new spring probably wants people to use the 'spring web flow' thingy.
3

you can use multi action controller .In Spring Single Controller Handel Multipal Method by method name resolver

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

Comments

1

https://jira.springsource.org/browse/SPR-5534

Comments

1

Question 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?

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.

You can create a servlet & servlet mapping that maps to your controller through the spring DispatcherServlet.

Create a servlet & servlet mapping to handle your requests.

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

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.

<?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
    }
}

Comments

0

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

Comments

0

you can use Spring webFlow and annotate your controller as @controller

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.