Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Handling multiple submit button

  1. #1
    Join Date
    Jul 2005
    Location
    COIMBATORE-INDIA
    Posts
    110

    Default Handling multiple submit button

    Hai springbrainees,

    In my jsp i am have submit buttons like save ,edit,delete.During onsubmit only one action will be taking palce(am i right) so how to write a controller to handle these mulitple request and change the process.

    Thank
    anieshuknair
    Have a Nice day

  2. #2
    Join Date
    Jul 2005
    Posts
    22

    Default use multiaction

    its simple.. use MultiActionController rather than SimpleFormController and you can write differnt methods for different in one single controller class.

    in your app-serverlet.xml .. make entries like this...
    --------------------
    <bean id="selectMethodResolver" class="org.springframework.web.servlet.mvc.multiac tion.ParameterMethodNameResolver">
    <property name="paramName">
    <value>param</value>
    </property>
    </bean>

    <bean id="yourController" class="com...YourController">
    <property name="methodNameResolver"><ref bean="selectGeoMethodResolver"/></property>
    </bean>

    <bean id="urlMapping" class="org.springframework.web.servlet.handler.Sim pleUrlHandlerMapping">
    <property name="mappings">
    <props>
    <prop key="/yourController.htm">yourController</prop>
    </props>
    </property>
    </bean>

    -----------------------------------
    in your jsp pass this "param as hidden field

    <form action="yourController.htm" method="post" >
    <input type="hidden" name="param" />



    now on each submit button...the best way to do ... like this

    <input type="submit" value="Save" onclick="save()"/>


    write a small javascript to submit the form to the appropiate method in your controller

    function setDispatch(dispatch) {
    document.forms[0].param.value = dispatch;
    }
    function save()
    {
    setDispatch('saveMethod'')
    }


    this "saveMethod" is the method name in your controller.. which will look like this

    public ModelAndView saveMethod
    (HttpServletRequest request, HttpServletResponse response) throws Exception
    {
    return new ModelAndView("JSP_name","model_name",model)
    }


    you can write any no of methods like this in this sigle controller

    any other thing

  3. #3
    Join Date
    Oct 2004
    Location
    Rotterdam, Netherlands
    Posts
    90

    Default

    Alternatively, in any Controller, you can use

    Code:
    WebUtils.hasSubmitParameter&#40;ServletRequest request,
                                             String name&#41;
    to check for any uniquely named submit button, and proceed from that.

    HTH, Thomas

  4. #4
    Join Date
    Sep 2005
    Location
    Delhi
    Posts
    7

    Default Re: use multiaction

    Hi Rahul,

    Does this method saveMethod acts simillar to the onSubmit in a SimpleFormController? I mean, do I do request.getParameter("myName") to get the user input as compared to the ((MyClass)command).getMyName() ? Will the control flow in the same way when the form submits? That is, First the Command object gets populated, goes to the Validator, and then goes to the onSubmit method? Or do I have to do the validations etc on my own?

    I would appreciate your input.
    Thank you.

    Regards,
    Harsh

  5. #5

    Default

    Hi Harsh,

    The saveMethod is similar to the OnSubmit in simpleformcontroller.But to get the populated command object in the method you will have to change the signature of the method.Add the command object as the third parameter to the method.Then you can access it as you did in the simpleformcontroller.

    As far as validator is concerned,you can map the validators to the multiactioncontroller methods based on the command object. See the api to see how to configure it.

    Regards
    harry

  6. #6
    Join Date
    Jul 2005
    Location
    COIMBATORE-INDIA
    Posts
    110

    Default

    Hai braniees,

    Thanks for all for u r reply.Let me try to use it in mycontroller.Currently i am using the onBing() method which gets the request.getParameter();

    form this i am able to get what the user has selected either save,edit as there are not submit buttons but simple buttons.I am using javascript submit the form.The code given by u all is a good alternate to me though the reply came lately.

    Regarding haris doubt

    U should use request.getParameter in the onBind event only.Which is called before validation.So validation will be made after the onbind.then after validation the onsubmit is called.onsubmit does not have request as its parameter.

    Thanks
    aniesh U.K

  7. #7
    Join Date
    Sep 2005
    Location
    Delhi
    Posts
    7

    Default

    Thank you all. I sincerly appreciate all the responses.

    I modified my Controller to extend from MultiActionController and my springapp-servlet.xml like this:

    <bean id="selectMethodResolver" class="org.springframework.web.servlet.mvc.multiac tion.ParameterMethodNameResolver">
    <property name="paramName">
    <value>param</value>
    </property>
    </bean>
    <bean id="priceIncreaseForm" class="com.admin.controller.PriceIncreaseFormContr oller">
    <property name="sessionForm"><value>true</value></property>
    <property name="commandName"><value>priceIncrease</value></property>
    <property name="commandClass"><value>com.admin.product.Price Increase</value></property>
    <property name="validator"><ref bean="priceIncreaseValidator"/></property>
    <property name="formView"><value>priceincrease</value></property>
    <property name="successView"><value>hello.htm</value></property>
    <property name="faliureView"><value>priceincrease.htm</value></property>
    <property name="productManager">
    <ref bean="prodMan"/>
    </property>
    <property name="methodNameResolver"><ref bean="selectMethodResolver"/></property>
    </bean>


    But now I get sessionForm is not writeable error. I understand that this error occurs when the framework is not able to find an appropriate setter method for the property. Is it that the MultiActionForm does not provide accessors for properties like sessionForm, commandName, etc? Will I have to provide my own methods for these or is there some other way to use them?

    error:
    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'priceIncreaseForm' defined in resource [/WEB-INF/springapp-servlet.xml] of ServletContext: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyExcep tion: Invalid property 'sessionForm' of bean class [com.admin.controller.PriceIncreaseFormController]: Property 'sessionForm' is not writable

  8. #8

    Default

    Hi Harsh,

    MultiActionController does not support form view.If you want it,you will have to write a custom controller.

    This post might help.

    http://forum.springframework.org/showthread.php?t=11086

    Regards
    Harry.
    Last edited by robyn; May 14th, 2006 at 07:54 PM.

  9. #9
    Join Date
    Sep 2005
    Location
    Delhi
    Posts
    7

    Default

    Thank you all for your help!!!

  10. #10
    Join Date
    Sep 2005
    Location
    Spain
    Posts
    21

    Default Using methodParamNames to handle multiple submit buttons

    The JavaDoc for ParameterMethodNameResolver says that you can use setMethodParamNames(String []) to map between form parameters and Controller methods. I.E, in a form:


    <INPUT type="submit" name="AddCustomer" value="Add">
    <INPUT type="submit" name="DeleteCustomer" value="Delete">

    I want AddCustomer() method called when button name AddCustomer gets pressed. But how do I it put in on the servlet-config.xml?

    <bean id="actionResolver" class="org.springframework.web.servlet.mvc.multiac tion.ParameterMethodNameResolver">
    <property name="paramName"><value>action</value></property>
    <property name="defaultMethodName"><value>ListCustomers</value></property>
    <property name="methodParamNames">
    [ AddCustomer parameter ] -> AddCustomer();
    [ DeleteCustomer parameter ] -> DeleteCustomer()
    </property>

    In theory it's possible without writting my own mapper class , isn't it?

Similar Threads

  1. Replies: 8
    Last Post: Oct 30th, 2012, 11:33 AM
  2. proper wizard back button handling
    By chav in forum Web Flow
    Replies: 4
    Last Post: Sep 12th, 2005, 11:11 AM
  3. Replies: 3
    Last Post: Aug 26th, 2005, 01:36 AM
  4. Two submit button with two captions
    By vijayan in forum Web Flow
    Replies: 1
    Last Post: Aug 26th, 2005, 01:24 AM
  5. Replies: 1
    Last Post: Mar 4th, 2005, 02:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •