Announcement Announcement Module
Collapse
No announcement yet.
Question on Multi-Action Controller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Question on Multi-Action Controller

    Hi everyone,

    Just curious what's the real benefit of a multi-action controller.

    According to the documentation, a multi-action controller is ideally used for mapping multiple entries to the same controller (i.e. grouping of similar functionalities). Borrowing the codes from the Spring documentation (below), we mappy multiple requests into "retrieveIndex" which is defined in the delegate class.

    =======COPY AND PASTE FROM SPRING DOCUMENTATION========
    <bean id="propsResolver" class="org....mvc.multiaction.PropertiesMethodName Resolver">
    <property name="mappings">
    <props>
    <prop key="/index/welcome.html">retrieveIndex</prop>
    <prop key="/**/notwelcome.html">retrieveIndex</prop>
    <prop key="/*/user?.html">retrieveIndex</prop>
    </props>
    </property>
    </bean>

    <bean id="paramMultiController" class="org....mvc.multiaction.MultiActionControlle r">
    <property name="methodNameResolver"><ref bean="propsResolver"/></property>
    <property name="delegate"><ref bean="sampleDelegate"/>
    </bean>

    public class SampleDelegate {

    public ModelAndView retrieveIndex(
    HttpServletRequest req,
    HttpServletResponse resp) {

    rerurn new ModelAndView("index", "date", new Long(System.currentTimeMillis()));
    }
    }
    =====COPY AND PASTE FROM SPRING DOCUMENTATION======

    I just wonder why do we bother using the multi-action controller when we can do something like:

    =========WITHOUT USING MULTI-ACTION CONTROLLER=====
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.Sim pleUrlHandlerMapping">
    <property name="mappings">
    <props>
    <prop key="/index/welcome.html">myController</prop>
    <prop key="/**/notwelcome.html">myController</prop>
    <prop key="/*/user?.html">myController</prop>
    </props>
    </property>
    </bean>
    <bean id="myController" class="com.web.controller.MyController">
    <property name="successView"><value>myView</value></property>
    </bean>
    =========WITHOUT USING MULTI-ACTION CONTROLLER=====

    As you can see in my example, MyController simply extends from "Controller" and multiple requests are mapped into this one controller.

    Maybe I've missed something, but I don't see any value of spending the extra effort to configure the application to use the multi-action controller.

    Thanks for your time.

  • #2
    The thing is that with a multi-action controller a single controller can handle multiple types of requests. The example in the docs is very bad since you are still handling only a single type of request ("retreiveIndex"), that's why you can indeed do the same with a normal controller as you show.

    A better example would be a shopping-cart multi-action controller. This would allow you to bundle all shopper-cart functionality together in a single class: listContents, addItem, removeItem, ...

    Ofcourse you would only want to do this if the functions you are bundling together are sufficiently simple, otherwise you would get a very large and complex multi-action controller.

    Erwin

    Comment


    • #3
      Just as a brief addition: with the MultiActionController you are actually mapping to different method calls; so you can put common functionality in a couple of protected methods, and then use those from more than one public method depending on the mapping (for instance CRUD type functionality). Of course there are other ways to accomplish the same thing, which depending on your design may be more appropriate.

      Comment


      • #4
        FYI: I found out a good example on below link

        http://www.goospoos.com/2009/11/spri...oller-example/

        Comment

        Working...
        X