Announcement Announcement Module
Collapse
No announcement yet.
Duplicate Form Submission Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Duplicate Form Submission

    Hi This is my first post.
    I want to avoid the duplicate form submission using Spring.I have followed this link http://forum.springsource.org/showthread.php?t=16733

    as per the link i had controllers like this
    Code:
    DisallowDupllicateFormController extends SimpleFormController{
    public DisallowDupllicateFormController() {
            //Initialize controller properties here or 
            //in the Web Application Context
            //setCommandClass(MyCommand.class);
            //setCommandName("MyCommandName");
            //setSuccessView("successView");
            //setFormView("formView");
        }
     protected ModelAndView disallowDuplicateFormSubmission(HttpServletRequest request, HttpServletResponse response)
                throws Exception {
            System.out.println("I am in disallow duplicate form");
            BindException errors = new BindException(formBackingObject(request), getCommandName());
            errors.reject("duplicateFormSubmission", null, "Duplicate form submission");
            return showForm(request, response, errors);
        }
    }
    
    UserDataController extends DisallowDupllicateFormController{
    public UserDataController() {
            // need a session to hold the formBackingObject
            setSessionForm(true);
    // initialize the form from the formBackingObject
            setBindOnNewForm(false);
            setValidateOnBinding(true);
            setCommandClass(Usermaster.class);
            setCommandName("userdata");
            
            
    @Override
        protected ModelAndView onSubmit(
                HttpServletRequest request,
                HttpServletResponse response,
                Object command,
                BindException errors) throws Exception {
    
    
            Usermaster usermaster = (Usermaster) command;
            ModelAndView mv = null;
    
            boolean updateSuccess = usermasterDAO.saveOrUpdate(usermaster);
            Map controlModel = null;
    
            if (updateSuccess == false) {
                return showForm(request, response, errors, controlModel);
    
            } else if (updateSuccess == true) {
                Map model = new HashMap();
                model.put("msg", "Updated Successfully..");
                return showForm(request, response, errors, model);
            }
    
            return mv;
        }
    
        @Override
        protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors) throws Exception {
            Usermaster form = (Usermaster) command;
        }
        }
    And in Jsp i added
    Code:
     <spring:bind path="userdata">
                <FONT color="red">
                    <B><c:out value="${status.errorMessage}"/></B>
                </FONT>
            </spring:bind>
    And dispatcher entry like this
    Code:
    <bean name="/usermasterdata.htm" class="com.airmiles.useradmin.controller.UserDataController">
            <property name="formView">
                <value>UserData</value>
            </property>
            <property name="successView">
                <value>UserData</value>
            </property>
            <property name="usermasterDAO" ref="userMasterDAO"/>
            <property name="secQuestionMasterDAO" ref="secQuestionMasterDAO"/>
        </bean>
    Here the problem is i am not able avoid the duplicate from submission and System.out.println("I am in disallow duplicate form") is not displayed when i clicked for the second time.

    Can some one guide me thank you in advance.........
    Last edited by vbolisetti; Jun 4th, 2010, 01:08 AM. Reason: Not getting responses

  • #2
    Can Sone One Give me resultion.

    Comment


    • #3
      How to aviod Duplicate Form Submission in sprin

      some one man..........respond for this..........got stuck up here

      Comment


      • #4
        Avoiding duplicate form submissions

        Originally posted by vbolisetti View Post
        some one man..........respond for this..........got stuck up here
        Hello,

        Have a look at this page section 3.3. I think it has the answer you are looking for.

        Kind regards

        Comment


        • #5
          Yes I tried that

          Hi,

          thank you for your reply i have implemented that but no result.that method is never called (i put sout statement it didn't display the message).
          if you can help me it is highly appreciable man

          thank you
          vijay kumar

          Comment


          • #6
            How to avoid Duplicate Form Submission in Spring

            Originally posted by vbolisetti View Post
            Hi,

            thank you for your reply i have implemented that but no result.that method is never called (i put sout statement it didn't display the message).
            if you can help me it is highly appreciable man

            thank you
            vijay kumar
            Hello, I am looking at the same problem at the moment on how to best implement that method efficiently. Basically I am thinking of doing something in the form of...

            Step 1. Overide isFormSubmission(request) to return "true" if request.getMethod() returns "POST" and to return "false" if request.getMethod() returns GET.
            According to Spring docs if the request method is a GET, the controller treats this as request to display a new form, while if the method is POST that is treated as a form submission and the controller will try to bind and process data to the form backing object.(Please correct me if I am wrong, but that is how I understand this so far).

            Code:
            @Override
            	protected boolean isFormSubmission(HttpServletRequest request) 
            	{
            		System.out.println("isFormSubmission Fired");
            		/*just debug output to check what comes in the request.
            		Enumeration myEnum = request.getSession().getAttributeNames();
            		while(myEnum.hasMoreElements())
            		{
            			System.out.println("Is Form Submission - Session Attribute" + 
            					myEnum.nextElement());
            		}
            		Enumeration myEnum2 = request.getAttributeNames();
            		while(myEnum2.hasMoreElements())
            		{
            			System.out.println("Is Form Submission - Request Attribute" + 
            					myEnum2.nextElement());
            		}
            		Enumeration myEnum3 = request.getParameterNames();
            		while(myEnum3.hasMoreElements())
            		{
            			System.out.println("Is Form Submission - Request Parameters" + 
            					myEnum3.nextElement());
            		}
            		*/
            		
            		System.out.println("The request method is: " + request.getMethod());
            		Boolean result;
            		if (request.getMethod()=="POST")
            		{
            			//We have form submit attempt here - POST
            			System.out.println("isFormSubmission - Detected POST");
            			result = true;
            		}
            		else
            		{
            			//We have a form display request here - GET
            			System.out.println("isFormSubmission - Detected GET");
            			result= false;
            		}
            		return result;
            		
            		//Boolean result = super.isFormSubmission(request);
            		//System.out.println("The form submission returned: " + result);
            		//System.out.println("The request method is: " + request.getMethod());
            		//return super.isFormSubmission(request);
            	}
            Step 2.
            Overide handleInvalidSubmit(request,response) to rteurn a new Model and View that displays the duplicate form submit warnig.
            Code:
                   @Override
            	protected ModelAndView handleInvalidSubmit(HttpServletRequest request, 
            	HttpServletResponse response) 
            	throws Exception 
            	{
            		BindException errors = getErrorsForNewForm(request);
            		errors.reject("duplicateFormSubmission", "Duplicate form submission");
            		System.out.println("handling Invalid Submit");
            		
            		String message = "Duplicate Form submission detected";
            		System.out.println(message);
            		ModelAndView duplicateSubmit = new ModelAndView("duplicateSubmit");
            		
            		//Add parameters to the new ModelAndView
            		System.out.println(Calendar.getInstance().getTimeInMillis() + 
            		"Adding params to new MAV");
            		
            		duplicateSubmit.addObject("message",message);
            		duplicateSubmit.addObject("request",request);
            		duplicateSubmit.addObject("response",response);
            		/*		
            		System.out.println(Calendar.getInstance().getTimeInMillis() + 
            		"Returning new MAV");
            		*/
            		return duplicateSubmit;
            	}
            Step 3:
            In the processFormSubmission() pass the request to the isFormSubmission, and according to the outcome, call the handleInvalidSubmit() or continue processing your data.
            Code:
            protected ModelAndView processFormSubmission(HttpServletRequest request, 
                    HttpServletResponse response, Object command, BindException errors) 
            	throws Exception 
            	{
            		//-Checking to see if this is a valid form submit request...
            		//if POST method is detected in request, then this is a 
            		//form Submit attempt.
            		if (this.isFormSubmission(request))
            		{
                                    //if command object is empty, then:
            			//POST with no form object.
            			//i.e. Possible duplicate submit state
            			if (command==null)
            			{
            				System.out.println(
            						"POST with no Form object Detected - " +
            						"Possible duplicate Submit");
            				//calls the handleInvalidSubmit() which is a 
                                            //ModelAndView type anyway
                                            return handleInvalidSubmit(request,response);
            			}
            		}
            		//if no duplicate submit is found, continue normal form processing
            		//TypeCast the command Object into a FormBackingObject class Object, in my case UploadMediaForm.
            		UploadMediaForm umf = (UploadMediaForm)command;
            
                            
                           //Call custom validators or use ValidationUtils to directly validate form input.
                           //-----------------------------Validation code here ------------------------------------// 
            
                           //create and return your success view if validation and processing completes successfully.
            
                          if(errors.hasErrors()||isFormChangeRequest(request))
            		{
            			return showForm(request,response,errors);
            		}
            		else
            		{
                                   ModelAndView addMediaSuccess = new ModelAndView("successView");
                                   addMediaSiccess.addObject("command",umf);
                                   return addMedia;
            
            
            
                            }
            Hope this helps

            Have a go and see if this code will work for you. Please let me know your results, as I am still trying to make this work properly with my App.
            Last edited by ta6hbe; Jun 17th, 2010, 05:53 AM.

            Comment


            • #7
              How to avoid Duplicate Form Submission in Spring

              Hi,

              Did any one find the solution to this prob?

              vbolisetti - Did you solve the problem after implementing the method stated by ta6hbe.

              -Rakesh Manoj

              Comment

              Working...
              X