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

  • Which controller should I use?

    Hi,
    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.
    Thanks.

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

    Comment


    • #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?

      Comment


      • #4
        hello:
        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.
        http://xxxx.xxx/student.do?task=add
        if you want to view a student's info.
        http://xxxx.xxx/student.do?task=view
        and then set the student controller like this:
        Code:
            <bean id="StudentController" 
                class="StudentController">
                <property name="methodNameResolver">
                    <ref local="StudentMethodNameResolver"/>
                </property>
            </bean>
        and using ParameterMethodNameResolver.
        Code:
           <bean id="StudentMethodNameResolver" 
                class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
                    <property name="paramName"><value>task</value></property>
                    <property name="defaultMethodName"><value>view</value></property> 
            </bean>
        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, 06:59 AM.

        Comment


        • #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.

          Comment


          • #6
            Thanks

            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?

            Comment


            • #7
              yes.in 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.

              Comment

              Working...
              X