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

  • SimpleFormController onSubmit()

    Can Anyone explain me the difference between onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) method and onSubmit(Object command) method of SimpleFormController?

    In particular I want to use the first method in my formcontroller because I need also of the HttpServletRequest object, but I can access only to the second method...

    SimpleFormController:
    Code:
    public class ModifyPasswordForm extends AbstractELabForm{
    	
        public ModifyPasswordForm(){
            setCommandClass(DeiStaff.class);
        }
    
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws ServletException{
             logger.info("onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)");
            logger.info("I want to be here!");
            return new ModelAndView("userView");
        }
    
    }
    Jsp Page:
    Code:
    <form method="post" action="<c&#58;url value="/modifyPasswordForm.htm"/>">
    <input value="Salva" name="command" type="submit">
    </form>

  • #2
    Well, I can't understand why it doesn't work.

    My task is simple, after the submit in the jsp I go to the formcontroller where I use the onSubmit method.

    Here I need also the HttpServletRequest so I used:
    Code:
    protected ModelAndView onSubmit&#40;HttpServletRequest request,
                    HttpServletResponse response, Object command, BindException errors&#41; 
            throws ServletException&#123;
    Map map = new HashMap&#40;&#41;;
    logger.info&#40;request.getParameter&#40;"username"&#41;&#41;;
    return new ModelAndView&#40;"userView","out",map&#41;;&#125;
    But after the jsp I'll go directly to the successView without entering in the method onSubmit.

    But if I use the default method onSubmit(Object command) I enter in the method and I can go to the userView jsp instead of the successView.
    This bypass of the successviewhas only the purpose to show if I enter the method onSubmit.

    Thanks to anyone who can help me!

    Comment


    • #3
      You should only override one onSubmit.

      Comment


      • #4
        Thank You yatesco, but I tried to override only the onSubmit that I need, but it doesn't work...

        I don't understand why if I use onSubmit with HttpServletRequest the method is bypassed and if I remove HttpServletRequest the method onSubmit is called and executed correctly!

        Comment


        • #5
          The variants of onSubmit in SimpleFormController aren't invoked separately in sequence but rather in a nested fashion, that is:

          onSubmit(request,response,command,errors)
          -> calls onSubmit(command,errors)
          -> calls onSubmit(command)

          So, because in your onSubmit(request,...) you return a new ModelAndView, the simpler onSubmits will never be called unless you call super.onSubmit(request,...).


          Cuong.

          Comment


          • #6
            Hi cuong, it seems from your post that the onSubmit(request,response,command,errors)
            it should be called, but in my case this is the problem!

            I put the method onSubmit(request,response,command,errors)
            in my formcontroller, but it it bypassed by the system.

            I don't want to use the simpler version of onSubmit(command), that works, but the onSubmit with request.

            Comment


            • #7
              Bear in mind that onSubmit is only called if there are no binding errors.

              Check to see if you have any validation or binding errors.

              Comment


              • #8
                I don't think so, in the jsp I have only few fields.

                And if I use the onSubmit(command) in the formcontroller, the methd is executed correctly so I suppose that there isn't errors...

                Comment


                • #9
                  Frizzi, can you post your entire controller code please

                  Comment


                  • #10
                    Formcontroller:
                    Code:
                    public class ModifyPasswordForm extends AbstractELabForm&#123;
                    	
                        public ModifyPasswordForm&#40;&#41;&#123;
                            setCommandClass&#40;DeiStaff.class&#41;;
                        &#125;
                    
                        protected ModelAndView onSubmit&#40;HttpServletRequest request,
                                    HttpServletResponse response, Object command, BindException errors&#41; 
                            throws ServletException&#123;
                        	Map map = new HashMap&#40;&#41;;
                        	logger.info&#40;request.getParameter&#40;"username"&#41;&#41;;
                            return new ModelAndView&#40;"userView","out",map&#41;;
                            &#125;
                    
                    &#125;
                    Jsp:

                    Code:
                    <h3>Modifica Password&#58;</h3>
                    					
                    						<table border="0" cellpadding="3" cellspacing="2">
                       							<tbody>
                    								<tr>
                    									<th>Username&#58;</th> 
                    									<td><c&#58;out value="$&#123;out.user.username&#125;"/></td>
                    								</tr>
                    								<tr>
                    									<th>Vecchia Password&#58;</th> 
                    									<td><input type="password" name="oldPassword" value="" maxlength="10"></td>
                    								</tr>
                    								<tr>
                    									<th>Nuova Password&#58;</th> 
                    									<td><input type="password" name="password" value="" maxlength="10"></td>
                    								</tr>
                    							</tbody>
                    						</table>
                    
                    
                    			  		    <form method="post" action="<c&#58;url value="/modifyPasswordForm.htm"/>">
                    						<input value="Salva" type="submit" name="User" onClick="return confirm&#40;'Attenzione&#58; i dati precedenti verranno eliminati e non sarą* pił possibile recuperarli'&#41;"> 
                    						<input name="idStaff" type="hidden" value="<c&#58;out value="$&#123;out.user.idStaff&#125;"/>">
                    						<input name="username" type="hidden" value="<c&#58;out value="$&#123;out.user.username&#125;"/>">
                    						</form>

                    Comment


                    • #11
                      You do realise that your onSubmit returns you to the successView, not your userView right

                      Comment


                      • #12
                        If you will take a look into Spring source code for the SimpleFormController, you will find that in entry method processFormSubmission a full-featered version of onSubmit method is called:

                        Code:
                        protected ModelAndView processFormSubmission&#40;
                        			HttpServletRequest request, HttpServletResponse response, Object command, BindException errors&#41;
                        			throws Exception &#123;
                        
                        		if &#40;errors.hasErrors&#40;&#41; || isFormChangeRequest&#40;request&#41;&#41; &#123;
                        			if &#40;logger.isDebugEnabled&#40;&#41;&#41; &#123;
                        				logger.debug&#40;"Data binding errors&#58; " + errors.getErrorCount&#40;&#41;&#41;;
                        			&#125;
                        			return showForm&#40;request, response, errors&#41;;
                        		&#125;
                        		else &#123;
                        			logger.debug&#40;"No errors -> processing submit"&#41;;
                        			return onSubmit&#40;request, response, command, errors&#41;;
                        		&#125;
                        	&#125;
                        So, if you overrided this version of onSubmit, than it will be executed, instead of default one, which actually calls the onSumbit(commonad, errors) version, which in turn calls the most simple version of onSubmit(), here is the stanza from source code of SimpleFormController.onSubmit methods:
                        Code:
                        	protected ModelAndView onSubmit&#40;
                        			HttpServletRequest request,	HttpServletResponse response, Object command,	BindException errors&#41;
                        			throws Exception &#123;
                        		return onSubmit&#40;command, errors&#41;;
                        	&#125;
                        Code:
                        	protected ModelAndView onSubmit&#40;Object command, BindException errors&#41; throws Exception &#123;
                        		ModelAndView mv = onSubmit&#40;command&#41;;
                        		if &#40;mv != null&#41; &#123;
                        			// simplest onSubmit version implemented in custom subclass
                        			return mv;
                        		&#125;
                        		else &#123;
                        			// default behavior&#58; render success view
                        			if &#40;getSuccessView&#40;&#41; == null&#41; &#123;
                        				throw new ServletException&#40;"successView isn't set"&#41;;
                        			&#125;
                        			return new ModelAndView&#40;getSuccessView&#40;&#41;, errors.getModel&#40;&#41;&#41;;
                        		&#125;
                        	&#125;

                        Comment


                        • #13
                          Sorry yatesco, the code that I posted was wrong.... :oops:

                          Now I correct it and the problem is always the same...

                          Why I can't use the onSubmit(request, command)??

                          Comment


                          • #14
                            Frizzi,

                            From your last post is the signature to your method onSubmit(HttpServletRequest,Object)? If this is the case then your method will never get run because it's not one of the overloads in SimpleFormContoller.

                            If it was just a typo then can I suggest the following steps:

                            1. In you onSubmit, add a line that calls the super.onSubmit passing exactly the parameters as your concrete method. This is a purely sanity-checking step to ensure your method is actually overriding. Remove the line if they're aren't any compilation errors.

                            2. If that fails, override the processFormSubmission method (it's not final so you can do it) using the exact same code as from source. You can then put in logging as well as step through it with Java debugging.

                            3. As a recent convert to JUnit and the power of mock objects, create a test case for your class. This can be especially useful in Eclipse with it's debugging capabilities rather than trying to do remote debugging.

                            Sorry it's not a solution but hopefully it'll help you see what's going in the framework itself.

                            Cuong.

                            Comment


                            • #15
                              found solution

                              I'm not sure if anyone will be following this thread, but I think I found a solution to the override problem. It really was simple, but very indirect.

                              In overriding the full onSubmit method, your have a BindException parameter. You have to make sure that you are getting the parameter from the org.springframework.validation.BindException jar. I was getting it from a different jar. When I changed it, it 'magically' worked.

                              Hope this helps

                              Comment

                              Working...
                              X