Announcement Announcement Module
Collapse
No announcement yet.
Redirecting back to the same form view after pressing submit Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Redirecting back to the same form view after pressing submit

    Code:
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
    
    		return new ModelAndView(new RedirectView(getSuccessView()));
    
    	}

    With the above code, when I submit the form it redirects to the correct URL, except the browser display a white blank page unless I refresh it then I get the form again.

    BTW - The success view is the form view iteself.

  • #2
    What is your successview? It should be the same url (or relative portion) that brought the person in originally from outside...

    Comment


    • #3
      Code:
      <bean id="searchController" class="com.me.web.SearchFormController">
      		<property name="sessionForm">
      			<value>false</value>
      		</property>
      		<property name="commandName">
      			<value>search</value>
      		</property>
      		<property name="commandClass">
      			<value>com.me.bo.Search</value>
      		</property>
      		<property name="formView">
      			<value>search</value>
      		</property>
      		<property name="successView">
      			<value>/browse/search.htm</value>
      		</property>
      		<property name="dBFacade">
      			<ref bean="dbfacade" />
      		</property>
      		<property name="pathMapping">
      			<ref bean="pathMapping" />
      		</property>
      	</bean>
      Code:
      <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
      		<property name="interceptors">
      			<list>
      				<ref bean="localeChangeInterceptor" />
      			</list>
      		</property>
      		<property name="mappings">
      			<props>
      				<prop key="/browse/search.htm">searchController</prop>
      
      snip...............

      When it redirects it gets a 404. when I press the browser refresh button, then the form is displayed again.

      Comment


      • #4
        You gave RedirectView a path that is relative to the contet, but by default RedirectView is not relative to the context. Use the constructor which has the boolean for relative flag, and set it to true. (alternately you could give a path that is fully absolute, or relative to the hostname, both of which would work, but are nowhere near as convenient).

        Colin

        Comment


        • #5
          I ended up using this and have no issues now. If this is not appropriate, I will use your suggestion.

          Code:
          protected ModelAndView onSubmit&#40;HttpServletRequest request, HttpServletResponse response, Object command, BindException errors&#41; throws Exception &#123;
          
          		return showForm&#40;request, errors, "search"&#41;;
          	&#125;

          Comment


          • #6
            Originally posted by 2devnull
            Code:
            		<property name="formView">
            			<value>search</value>
            		</property>
            		<property name="successView">
            			<value>/browse/search.htm</value>
            		</property>
            Shouldn't that be...

            Code:
            		<property name="formView">
            			<value>search</value>
            		</property>
            		<property name="successView">
            			<value>search</value>
            		</property>

            Comment


            • #7
              I'm not sure. I have been doing it this way since I installed Spring 1.0.2 and used the examples to get started. I had the same question why the path was being duplicated here but did not follow up on it since it worked and got me going. Right now, I am actually using XML <!ENTITY's so as to maintain the path in one place for the xxxx-servlet.xml file. So actualy, this is what I have:

              Code:
              <!ENTITY path.browse.search "/browse/search.htm">

              Code:
              <property name="formView">
                       <value>search</value>
                    </property>
                    <property name="successView">
                       <value>&path.browse.search;</value>
              </property>
              Code:
              <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
              		<property name="interceptors">
              			<list>
              				<ref bean="localeChangeInterceptor" />
              			</list>
              		</property>
              		<property name="mappings">
              			<props>
              				<prop key="&path.browse.search;">searchController</prop>

              Code:
              <bean id="pathMapping" class="java.util.HashMap">
              		<constructor-arg>
              			<map>
              				<entry key="path.browse.search"><value>&path.browse.search;</value></entry>
              
              This is use for my breadcrumbs.

              Comment


              • #8
                showForm is better in any case, as with the RedirectView you are putting awareness into your controller that the successview is actually an html URL, not a great thing...

                Comment


                • #9
                  Shouldn't that be...
                  Code:
                  <property name="formView">
                     <value>search</value>
                  </property>
                  <property name="successView">
                     <value>search</value>
                  </property>
                  You wouldn't do that unless you were using an actual view name (such as "search.jsp"), and in that case you wouldn't need to redirect. In this case it is a URL such as those seen by the end user in the address bar of the browser window.

                  --

                  Code:
                  protected ModelAndView onSubmit&#40;HttpServletRequest request, HttpServletResponse response, Object command, BindException errors&#41; throws Exception &#123;
                  
                        return showForm&#40;request, errors, "search"&#41;;
                  &#125;
                  This is a good way to do it because any logic in your showForm() method will always be executed before the form is rendered. There is a difference between the showForm() and the redirect though. Choose the best for your particular situation:

                  1. showForm() will just show the form. If the user refreshes the page it will resubmit the form data submitted with the original request that ended with the execution of onSubmit().

                  2. Redirect will tell the user's browser to send a new request for "/browse/search.htm". Consequently, if the page is refreshed there is no form data to resubmit--the browser just executes a normal GET request for "/browse/search.htm".

                  Comment


                  • #10
                    Is there some way to redirect to *another* page using showForm() ?, keeping the concept of redirects out of the controller.

                    If you call showForm() from within controllerA to try to display viewB (a JSP pages which displays errors within <spring:bind> tags), then an error is received (below).


                    Could not find Errors instance for bean [viewB] in request: add the Errors model to your ModelAndView via errors.getModel()


                    There doesn't seem to be any way around this if controllerA and controllerB use different 'commandName' values


                    Doing the following in the processFormSubmission() method seems to redirect back to another page ok, but like you say now means the controller knows about redirects, etc.

                    Code:
                    return new ModelAndView&#40;new RedirectView&#40;"/viewB.view", true&#41;, errors.getModel&#40;&#41;&#41;;
                    Also, why does RedirectView take a URL in it's constructor instead of a view name which could then be resolved by a ViewResolver or something?

                    Thanks.

                    Comment

                    Working...
                    X