Announcement Announcement Module
Collapse
No announcement yet.
Handling multiple submit button Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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

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

    Comment


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

      Comment


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

        Comment


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

          Comment


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

            Comment


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

              Comment


              • #8
                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, 07:54 PM.

                Comment


                • #9
                  Thank you all for your help!!!

                  Comment


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

                    Comment


                    • #11
                      I was expecting an easy answer here, now I had to read the documentation.
                      So for the next lazy person..... here it is:

                      <bean id="actionResolver" class="org.springframework.web.servlet.mvc.multiac tion.ParameterMethodNameResolver">
                      <property name="defaultMethodName"><value>ListCustomers</value></property>
                      <property name="methodParamNames">
                      <list>
                      <value>AddCustomer</value>
                      <value>DeleteCustomer</value>
                      </list>
                      </property>
                      </bean>

                      Comment


                      • #12
                        hi rahul,
                        As you stated:

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

                        but what exactly is the property here?? I am trying a small application in Spring, which should handle multiple submission from same jsp page to same Controller. I think the solution you described would ideally match for me, but I'm not sure about the property name or ref bean!

                        Thanks,
                        Minu.

                        Comment


                        • #13
                          Originally posted by rahul_maggu View Post
                          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
                          Based on your suggestion to add command object to access form variables in the controller, I added the following:

                          <c:url value='/aaa/bbb.htm'/>
                          <form:form method="POST" action='/aaa/bbb.htm' commandName="customer">

                          <form:input path="fristname" />

                          <form:input path="lastname" />
                          ....

                          </form:form>

                          When this form is submitted, my CustomerController.getCustomerInfo() is called as per
                          http://forum.springframework.org/showthread.php?t=16209.
                          CustomerController is a multiactioncontroller.

                          public ModelAndView getCustomerInfo(HttpServletRequest request, HttpServletResponse response, Object command)
                          throws ServletException, IOException {
                          ..
                          Customer customer = (Customer)request.getAttribute("customer");
                          ...

                          }

                          When I retrieve the 'customer', I am always getting it as null. How do I get the form variables that
                          are set to commandName="customer" in controller class method?

                          1. Is the passing of "Object command" as third param correct?
                          2. Would the following stmt carry input params from the form fields to controller method when form is
                          submitted?
                          <form:form method="POST" action='/aaa/bbb.htm' commandName="customer">

                          I am not sure how I can access the form variables in multiactioncontroller method?

                          Can anybody has any code examples?

                          Comment


                          • #14
                            MultiActionController - Retrieving Command Object

                            When I retrieve the 'customer', I am always getting it as null. How do I get the form variables that
                            are set to commandName="customer" in controller class method?
                            [CODE]Customer customer = (Customer) request.getAttribute("customer");/CODE]
                            You missed the point. After adding the 3rd parameter (Command object), you should as well use it

                            Code:
                            Customer customer = (Customer) command;

                            Comment


                            • #15
                              sorry in advance for hijacking the thread.....


                              hi i am new to spring please help me..

                              this is my servlet file:

                              <bean class="org.springframework.web.servlet.mvc.support .ControllerClassNameHandlerMapping"/>

                              <!--
                              Most controllers will use the ControllerClassNameHandlerMapping above, but
                              for the index controller we are using ParameterizableViewController, so we must
                              define an explicit mapping for it.
                              -->
                              <bean id="urlMapping" class="org.springframework.web.servlet.handler.Sim pleUrlHandlerMapping">
                              <property name="mappings">
                              <props>
                              <prop key="index.htm">indexController</prop>
                              <prop key="prodInfo">productController</prop>
                              </props>
                              </property>
                              </bean>

                              <bean id="viewResolver" class="org.springframework.web.servlet.view.Intern alResourceViewResolver"
                              prefix="/WEB-INF/jsp/"
                              p:suffix=".jsp" />

                              <!--
                              The index controller.
                              -->
                              <bean name="indexController" class="org.springframework.web.servlet.mvc.Paramet erizableViewController"
                              p:viewName="index" />
                              <!-- My Controllers -->
                              <bean class="Controller.ProductController" productService-ref="productService"/>
                              <bean class="Controller.CustomerOrderController" p:customerOrderService-ref="customerorderService" />
                              <bean class="Controller.EmployeeController" p:employeeService-ref="employeeService" />
                              <bean class="Controller.SupplierController" p:supplierService-ref="supplierService" />
                              <bean class="Controller.BuyController" p:buyService-ref="buyService" />
                              </beans>

                              i was using simpleForm controllers, but now i have to use multiaction controller as i have multiple forms...but i have no idea how to set them in the dispatcher....please help...

                              Comment

                              Working...
                              X