Announcement Announcement Module
Collapse
No announcement yet.
Newbie: Problems mapping successView w/SimpleFormController Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Newbie: Problems mapping successView w/SimpleFormController

    Hi.

    I seem to be missing something simple. I'm getting an HTTP 404 error upon redirecting to my successView after successful processing of the form controller's onSubmit method.

    I'm hooking up Spring to an existing JSP-based website. I have mapped the SpringDispatcher servlet to /dyn/* in my WEB-INF/web.xml:
    Code:
        <servlet>
            <servlet-name>spring</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/classes/spring-servlet.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>spring</servlet-name>
            <url-pattern>/dyn/*</url-pattern>
        </servlet-mapping>
    I have a SimpleFormController subclass called RegistrationFormController. Here's my whole spring-servlet.xml:
    Code:
    <beans>
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
            <property name="prefix"><value>/WEB-INF/jsp/</value></property>
            <property name="suffix"><value>.jsp</value></property>
        </bean>
    
        <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="basename"><value>messages</value></property>
        </bean>
    
        <bean id="registrationCommandValidator" class="xcalia.xdn.site.RegistrationCommandValidator"/>
    
        <bean id="registrationFormController" class="xcalia.xdn.site.RegistrationFormController">
            <property name="sessionForm"><value>true</value></property>
            <property name="commandName"><value>registrationCommand</value></property>
            <property name="commandClass"><value>xcalia.xdn.site.RegistrationCommand</value></property>
            <property name="validator"><ref bean="registrationCommandValidator"/></property>
            <property name="formView"><value>registrationform</value></property>
            <property name="successView"><value>redirect&#58;registered</value></property>
        </bean>
    
        <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <props>
                    <prop key="/register">registrationFormController</prop>
                </props>
            </property>
        </bean>
    
    </beans>
    The problem that I'm having is that upon successful processing of my form in the RegistrationFormController.onSubmit method, Spring can't seem to find my view JSP, located at WEB-INF/jsp/registered.jsp. Here's my onSubmit method:
    Code:
        protected ModelAndView onSubmit&#40;
                HttpServletRequest request,
                HttpServletResponse response,
                Object commandObject,
                BindException bindException&#41; throws Exception &#123;
    
            RegistrationCommand rc = &#40;RegistrationCommand&#41; commandObject;
    
            UUIDGenerator uuidGenerator = UUIDGenerator.getInstance&#40;&#41;;
            UUID uuid = uuidGenerator.generateTimeBasedUUID&#40;&#41;;
            String confirmationCode = uuid.toString&#40;&#41;;
    
            request.getSession&#40;&#41;.setAttribute&#40;"registrationCommand", rc&#41;;
            request.getSession&#40;&#41;.setAttribute&#40;"confirmationCode", confirmationCode&#41;;
    
            // using RedirectView to ensure that double-posting can't be configured accidentally
            return new ModelAndView&#40;new RedirectView&#40;getSuccessView&#40;&#41;, true&#41;&#41;;
        &#125;
    I thought that my InternalResourceViewResolver should be able to find the view, but it's not, and I'm getting an HTTP 404 error. Here's a portion of the DEBUG-level the log:
    Code:
    2005-07-19 09&#58;39&#58;12,877 DEBUG &#91;org.springframework.web.servlet.view.JstlView&#93; - <Forwarded to resource &#91;/WEB-INF/jsp/registrationform.jsp&#93; in InternalResourceView 'registrationform'>
    2005-07-19 09&#58;39&#58;12,877 DEBUG &#91;org.springframework.web.servlet.DispatcherServlet&#93; - <Successfully completed request>
    2005-07-19 09&#58;39&#58;12,877 DEBUG &#91;org.springframework.web.context.support.XmlWebApplicationContext&#93; - <Publishing event in context &#91;WebApplicationContext for namespace 'spring-servlet'&#93;&#58; RequestHandledEvent&#58; url=&#91;/dyn/register&#93;; time=&#91;0ms&#93;; client=&#91;127.0.0.1&#93;; method=&#91;GET&#93;; servlet=&#91;spring&#93;; session=&#91;51C1D65FBA95F3CE98417329F3E58715&#93;; user=&#91;null&#93;; status=&#91;OK&#93;>
    2005-07-19 09&#58;39&#58;40,436 DEBUG &#91;org.springframework.web.servlet.DispatcherServlet&#93; - <DispatcherServlet with name 'spring' received request for &#91;/dyn/register&#93;>
    2005-07-19 09&#58;39&#58;40,436 DEBUG &#91;org.springframework.web.servlet.DispatcherServlet&#93; - <Testing handler map &#91;[email protected]e7958&#93; in DispatcherServlet with name 'spring'>
    2005-07-19 09&#58;39&#58;40,436 DEBUG &#91;org.springframework.web.servlet.handler.SimpleUrlHandlerMapping&#93; - <Looking up handler for &#91;/register&#93;>
    2005-07-19 09&#58;39&#58;40,436 DEBUG &#91;org.springframework.web.servlet.DispatcherServlet&#93; - <Testing handler adapter &#91;org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter@cb6a34&#93;>
    2005-07-19 09&#58;39&#58;40,446 DEBUG &#91;org.springframework.beans.CachedIntrospectionResults&#93; - <Using cached introspection results for class &#91;xcalia.xdn.site.RegistrationCommand&#93;>
    2005-07-19 09&#58;39&#58;40,446 DEBUG &#91;org.springframework.beans.BeanWrapperImpl&#93; - <About to invoke write method &#91;public void ...
    
    &#40;snip&#41;
    
    &#91;org.springframework.validation.ValidationUtils&#93; - <Invoking validator &#91;xcalia.xdn.site.RegistrationCommandValidator@146ac5a&#93;>
    2005-07-19 09&#58;39&#58;40,466 DEBUG &#91;org.springframework.validation.ValidationUtils&#93; - <Validator found no errors>
    2005-07-19 09&#58;39&#58;40,476 DEBUG &#91;org.springframework.web.servlet.DispatcherServlet&#93; - <Rendering view &#91;org.springframework.web.servlet.view.RedirectView&#58; unnamed; URL &#91;registered&#93;&#93; in DispatcherServlet with name 'spring'>
    2005-07-19 09&#58;39&#58;40,476 DEBUG &#91;org.springframework.web.servlet.view.RedirectView&#93; - <Rendering view with name 'null' with model null and static attributes &#123;&#125;>
    2005-07-19 09&#58;39&#58;40,486 DEBUG &#91;org.springframework.web.servlet.DispatcherServlet&#93; - <Successfully completed request>
    2005-07-19 09&#58;39&#58;40,486 DEBUG &#91;org.springframework.web.context.support.XmlWebApplicationContext&#93; - <Publishing event in context &#91;WebApplicationContext for namespace 'spring-servlet'&#93;&#58; RequestHandledEvent&#58; url=&#91;/dyn/register&#93;; time=&#91;50ms&#93;; client=&#91;127.0.0.1&#93;; method=&#91;POST&#93;; servlet=&#91;spring&#93;; session=&#91;51C1D65FBA95F3CE98417329F3E58715&#93;; user=&#91;null&#93;; status=&#91;OK&#93;>
    2005-07-19 09&#58;39&#58;40,497 DEBUG &#91;org.springframework.web.servlet.DispatcherServlet&#93; - <Testing handler map &#91;[email protected]e7958&#93; in DispatcherServlet with name 'spring'>
    2005-07-19 09&#58;39&#58;40,497 DEBUG &#91;org.springframework.web.servlet.handler.SimpleUrlHandlerMapping&#93; - <Looking up handler for &#91;/registered&#93;>
    2005-07-19 09&#58;39&#58;40,507 DEBUG &#91;org.springframework.web.servlet.DispatcherServlet&#93; - <No handler found in getLastModified>
    2005-07-19 09&#58;39&#58;40,507 DEBUG &#91;org.springframework.web.servlet.DispatcherServlet&#93; - <DispatcherServlet with name 'spring' received request for &#91;/dyn/registered&#93;>
    2005-07-19 09&#58;39&#58;40,507 DEBUG &#91;org.springframework.web.servlet.DispatcherServlet&#93; - <Testing handler map &#91;[email protected]e7958&#93; in DispatcherServlet with name 'spring'>
    2005-07-19 09&#58;39&#58;40,507 DEBUG &#91;org.springframework.web.servlet.handler.SimpleUrlHandlerMapping&#93; - <Looking up handler for &#91;/registered&#93;>
    2005-07-19 09&#58;39&#58;40,507 WARN &#91;org.springframework.web.servlet.PageNotFound&#93; - <No mapping for &#91;/dyn/registered&#93; in DispatcherServlet with name 'spring'>
    2005-07-19 09&#58;39&#58;40,507 DEBUG &#91;org.springframework.web.servlet.DispatcherServlet&#93; - <Successfully completed request>
    2005-07-19 09&#58;39&#58;40,507 DEBUG &#91;org.springframework.web.context.support.XmlWebApplicationContext&#93; - <Publishing event in context &#91;WebApplicationContext for namespace 'spring-servlet'&#93;&#58; RequestHandledEvent&#58; url=&#91;/dyn/registered&#93;; time=&#91;0ms&#93;; client=&#91;127.0.0.1&#93;; method=&#91;GET&#93;; servlet=&#91;spring&#93;; session=&#91;51C1D65FBA95F3CE98417329F3E58715&#93;; user=&#91;null&#93;; status=&#91;OK&#93;>
    Can someone please tell me what I'm missing?

    Thanks,
    Matthew

  • #2
    Matthew,

    Returning a redirect view with redirect:registered is going to create an internal 302 redirect to a resource called registered under the current path.

    This redirect will go all the way back through the servlet stack until it hits Spring again at which time Spring will look for a controller (not a View) that can handle that mapped URL.

    Try putting a simple Controller in front of that view and mapping it to /registered.

    Rob

    Comment


    • #3
      Thanks for the help; putting the success view JSPs outside of WEB-INF worked.

      That raises a question -- one of the reasons for putting certain pages under WEB-INF is to prevent arbitrary access to them. By keeping my formView pages under WEB-INF, I can do just that. But what about my successView pages? It seems that they should be entitled to the same protection. At this point, anyone can request a successView page via their address bar, like http://localhost:8080/confirmed.jsp, and it will totally bomb.

      Is there some design-type reason that the InternalResourceViewResolver won't render successView pages that are found under WEB-INF? I still feel like I'm missing something... :?

      Thanks,
      Matthew

      Comment


      • #4
        Matthew,

        InternalResourceViewResolver will resolve views inside WEB-INF, however, IRVR is *not* responsible for directly resolving the view "redirect:registered".

        What will happen is that Spring will issue an HttpServletResponse.sendRedirect("registered") which will redirect the current request to /dyn/registered. From there it will hit DispatcherServlet again and Spring will attempt to resolve a controller for /registered and it won't find one. What you can do is either remove the "redirect:" prefix so you get an actual view resolved by IRVR without the redirect or map a simple Controller to /registered:

        Code:
        public classs MyController implements Controller &#123;
           public ModelAndView handleRequest&#40;HttpServletRequest request, HttpServletResponse response&#41; throws Exception &#123;
        			return new ModelAndView&#40;"registered"&#41;;
        		&#125;
        &#125;
        Rob

        Comment


        • #5
          Issues with url in browser

          Hi,

          I have just done the above (implemented a dummy controller for the redirect) but found the resulting url in the browser has extra info on the end:

          Code:
          ?org.springframework.validation.BindException.command=org.springframework.validation.BindException%3A+BindException%3A+0+errors&command=com.service.Query%401e70a5a
          Is it possible to have that removed? There are no errors thrown that I can see in the controller (or the logs).

          Thanks

          Rakesh

          Comment


          • #6
            Originally posted by robh View Post
            Matthew,

            InternalResourceViewResolver will resolve views inside WEB-INF, however, IRVR is *not* responsible for directly resolving the view "redirect:registered".

            What will happen is that Spring will issue an HttpServletResponse.sendRedirect("registered") which will redirect the current request to /dyn/registered. From there it will hit DispatcherServlet again and Spring will attempt to resolve a controller for /registered and it won't find one. What you can do is either remove the "redirect:" prefix so you get an actual view resolved by IRVR without the redirect or map a simple Controller to /registered:

            Code:
            public classs MyController implements Controller {
               public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
            			return new ModelAndView("registered");
            		}
            }
            Rob
            Rob, I am having a similar problem but I do not want to do a redirect. I simply want to render a jsp in case of success. Apparently, SimpleFormController successView completely ignores my ViewResolver.

            After a successful submit, I get the following error:

            HTTP Status 404 - The requested resource (/testapp/userForm) is not available.

            Why won't the view resolver resolve this jsp??

            Code:
            <bean id="userFormController" class="web.controllers.UserFormController">
            	<property name="sessionForm" value="true"/>
            	<property name="bindOnNewForm" value="false"/>
            	<property name="commandName" value="userCmd"/>
            	<property name="commandClass" value="web.command.UserCommand"/>
            	<property name="validator" ref="userValidator" />
            	<property name="formView" value="userForm"/>
            	<property name="successView" value="userForm"/>
            </bean>
            
            <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            	<property name="viewClass">
            		<value>org.springframework.web.servlet.view.JstlView</value>
            	</property>
            	<property name="prefix">
            		<value>/WEB-INF/jsp/</value>
            	</property>
            	<property name="suffix">
            		<value>.jsp</value>
            	</property>
            </bean>

            Comment

            Working...
            X