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

  • SimpleFormController Issue

    I am trying to implement a SimpleFormController where I need to implement my own onSubmit method to get data from my server side code. For some reason the SimpleFormController is not calling my overridden method. I have tried overriding all the various methods as suggested in the API / docs. Has anyone seen this before? If so, what am I doing wrong? Here is my code.

    Code:
    public class SearchProductFormController extends SimpleFormController {
    	// init binder to do formatting on the dates
    	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
    		SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
    		dateFormat.setLenient(false);
    		binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, false));
    	}
    	
    protected ModelAndView onSubmit(Object command, BindException e) throws Exception { 
    	SearchCriteria criteria = (SearchCriteria) command;
    	ReportDelegate delegate = new ReportDelegateImpl();
    	LeadReportHeader report =  delegate.getSearchLeadProducts(criteria);
    	 return new ModelAndView("productReport", "report", report);
    	}
    }
    Code:
    		 <bean id="productSearchController" class="web.controllers.SearchProductFormController">
    			<property name="commandClass"><value>server.reporting.SearchCriteria</value></property>
       	  <property name="formView"><value>index</value></property>
       	  <property name="sessionForm"><value>true</value></property>
    	<property n ame="successView"><value>productReport</value></property>
    			<property name="validator"><ref bean="searchValidator" /></property>
        </bean>
    The result of the above code is just forwarding to my successView without calling my onSubmit method at all.

    Thanks in advance!

  • #2
    For some reason the SimpleFormController is not calling my overridden method.
    Are you submitting as a POST method or overriding isFormSubmission correctly?

    Comment


    • #3
      The form I am submitting is using the post method.

      Comment


      • #4
        I guess
        <property n ame="successView"><value>productReport</value></property>
        is just a typo in this post?

        Comment


        • #5
          Yes, that it is just a spacing issue that occurred when pasting my code into the post.

          Comment


          • #6
            Also, the way that I am submitting this form in my jsp is via a JavaScript call.

            Code:
            function goSubmit&#40;&#41;&#123;
            var form = document.getElementById&#40;"criteriaForm"&#41;;
            form.submit&#40;&#41;;
            &#125;
            Would this produce any strange behavior like I am experiencing?

            Thanks for the help!!

            Comment


            • #7
              What does the jsp look like? BTW; a "submission" is not neccessarily a POST

              Comment


              • #8
                Here is some code from the .jsp and the javascript that submits the form.

                Code:
                <table width="100%">
                		<form id="criteriaForm" name="criteriaForm" action="<c&#58;url value="/search.htm" />" method="POST">
                
                // LEFT THE OTHER STUFF OUT
                
                <a href="javascript&#58;submitSearch&#40;&#41;">RUN REPORT</a>
                </form>
                </table>
                Code:
                function submitSearch&#40;&#41;&#123;
                 var list = document.getElementById&#40;"reportlist"&#41;;
                  if &#40;list.options&#91;list.selectedIndex&#93;.value != ""&#41;&#123;
                		var form = document.getElementById&#40;"criteriaForm"&#41;;
                		form.submit&#40;&#41;;
                	&#125; else &#123;
                		alert&#40;"You must choose a report to run"&#41;;
                	&#125;
                &#125;
                Any feedback would be appreciated.

                THANKS!!

                Comment


                • #9
                  Code looks OK. It appears it should be doing a POST. Is your URL resolving configuration correct?

                  Comment


                  • #10
                    Yes, the URL configuration mapping is correct. On the submit action I do get forwarded to the successView as I should. Just that my overridden onSumbit was not called

                    Comment


                    • #11
                      Write a unit test...

                      I don't have the answer for you, but in order to eliminate any doubt about the JavaScript or your JSP in general, I'd suggest you write a unit test that creates and submits a mock request to your controller. I use JUnit with MockRunner http://mockrunner.sourceforge.net to do this. That's the beauty of using IoC, everything's mockable and therefore unit testable in isolation from everything else (sorry if this is teaching you to suck eggs).

                      Even once you've fixed the bug, you will still benefit from having that unit test run automatically every time you build your project.

                      Comment


                      • #12
                        On the submit action I do get forwarded to the successView as I should.
                        What code is doing the forwarding then? Are you sure the method is not executing? Maybe there is no data in your 'report' variable. Can you set up a remote debugger, or put in some debug statements to verify this.

                        Comment


                        • #13
                          I do have a System.out in my overridden onSubmit method, I just didn't inlcude that when I posted the code. But believe me it is not getting called. It just seems that the default onSubmit method is just forwarding to the succesView (DI) after the form submits and the bind is successful.

                          Comment


                          • #14
                            Well, I fixed the issue. For some reason not having a sumit button in my form is what was causing this. As you can see I had the following Javascript submitting my form.

                            Code:
                            <a href="javascript&#58;submitSearch&#40;&#41;">RUN REPORT</a>
                            So then I added a standard submit button to the form and now it works.

                            Code:
                            <input type="submit" name="report" value="Run Report" onclick="javascript&#58;submitSearch&#40;&#41;" />
                            So, I wonder what is causing this? Does the spring controller framework look for some HTTP header information that is not put into the header when using form.submit() without the submit button in the form? I got this to work but I still don't understand this behavior. Anybody have any thoughts?

                            Thanks for all the suggestions!!

                            Comment


                            • #15
                              Does the spring controller framework look for some HTTP header information that is not put into the header when using form.submit() without the submit button in the form?
                              By default Spring treats a POST submission as a form submission - as stated above you can override this with isFormSubmission.

                              I got this to work but I still don't understand this behavior.
                              I'm not sure what's going on in your case either. :?

                              You can probably track this down by moitoring what actually gets submitted. You can view the network traffic, or just change the method to a GET and inspect the variables in the URL (of course it shouldn't be treated as a submit in this case).

                              Comment

                              Working...
                              X