Announcement Announcement Module
Collapse
No announcement yet.
one controller, multiple forms Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • one controller, multiple forms

    I'm looking at Spring MVC, and trying to figure out if this is possible...

    I have an application with multiple forms, each that take a different object, but I want to use a single controller ie; I want a SaveObjectController that doesn't care if it's passed a PersonObject, a DogObject, or a CarObject (each object being saved from a different form).

    I was able to do it in Struts using DynaActionForms, and having all my DTO's extend from a BaseDAO (it was a little more complex, but minimized the number of Action classes needed). Because of the initBinder method, it doesn't appear that this is possible with Spring MVC; it looks like it's a case of one controller per object, even if everything but the databinding is the same.

    Is this the case, or am I missing something?

    Thanks.

  • #2
    Well, it is doable if you wrap all ur DTO Objects (Person/Dog/Cat) inside another class and expose that object as the one that would be bound to your form. This way, your controller will have only a single class containing an object which can be Person/Dog/Cat. So the object that you bind in initBinder will be
    public class ObjectHolder
    {
    private Object obj;
    <Setters, Getters>
    }
    In your forms, you can reference the fields as obj.catName(assuming your commandName is "obj").

    Comment


    • #3
      Have you looked at MultiActionController? It will handle different types of command objects.

      Comment

      Working...
      X