Announcement Announcement Module
No announcement yet.
Which controller should I use? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Which controller should I use?

    I am new to Spring. Just wanted to know can I do this with a single controller.
    - when my page loads a list of students will get displayed
    - each has a Edit link
    - page has a add link
    - if click on edit form will come with populated values for that student
    - if add then same from without any values
    - should be able to bind command and validator class with controller
    - save should same for both

    I could not do this in a single controller, please let me know if can be done.

  • #2
    i also face same problem which one to select from the rich set of spring controller

    by running pet clinic application i have observed that developers used one controller for one form which is subclass of SimpleFormController

    if u wants to work with link then i think multiaction will be better option


    • #3
      I cannot use one

      Hi Ganesh,
      Thanks for your reply, so you are saying its not possible to use a single controller for all the functionalities I mentioned?


      • #4
        you can using the MultiActionController and it has bind(request,commandobj)
        method.this method can put page form into ur commandobj.
        and it also has a method setValidators(Validator[] validators) to set a validator class for ur commandobj.
        you should set the url with the list of student like this:
        if you want to add a student.
        if you want to view a student's info.
        and then set the student controller like this:
            <bean id="StudentController" 
                <property name="methodNameResolver">
                    <ref local="StudentMethodNameResolver"/>
        and using ParameterMethodNameResolver.
           <bean id="StudentMethodNameResolver" 
                    <property name="paramName"><value>task</value></property>
                    <property name="defaultMethodName"><value>view</value></property> 
        and than you can set some method which the name is same as the parameter
        after the task in the class extends MultiActionController.
        for example task=add.there should be a method named add.and the dispatcherservlet will call the add method if the parameter is the add after the task.
        at last in the method,you can using the bind method and set a validator.

        hope this useful for you.

        Last edited by Oranger; Dec 29th, 2005, 05:59 AM.


        • #5
          You *could* do it all with one controller if your needs are very simple, but right off the bat you are basically complicating things by having a list view and form view. Populating two disparate views is not really the job of one controller - I suggest you at least split them into two.

          Even adding and editing can change the entire context of a workflow and view - non-editable fields, permission checking on edits, duplicate checking, changing messaging, and similar business rules can overwhelm a single controller and view. If you start with one, be prepared to refactor.


          • #6

            Hi everyone,
            Thanks to all of you for very useful information.

            Oranger - is bind() method called by the framework, for me I did not get it
            called by framework and I called it from each method. Is this correct?


            • #7
     SimpleFormController the bind() method called by the framework itself.

              and here you should call it from each method or you can set a methodbeforeinterceptor.before the controller is called , bind the request to the commandobj.