Announcement Announcement Module
Collapse
No announcement yet.
PriceIncreaseFormController from the MVC tutorial in the doc Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • PriceIncreaseFormController from the MVC tutorial in the doc

    After getting the price increase form working from the MVC tutorial I read the docs of SimpleFormController and thought that:

    public ModelAndView onSubmit(Object command)
    throws ServletException {

    ... // increase logic
    return new ModelAndView(new RedirectView(getSuccessView()));
    }

    could be replaced with:

    protected void doSubmitAction(Object command) throws Exception(
    ... // increase logic
    // no need for the return of ModelAndView(...)
    }


    Now when I tried this and submit a price increase I get a web server error that /WEB-INF/jsp/hello.htm.jsp is missing (hello.htm would have been resolved just fine with the servlet mapping). What I can't figure out is where the .jsp at the end got added and therefore why this didn't work. I expected them to be equivalent.

    Can someone enlighten me?

  • #2
    sprior,

    In this post I will refer to springapp/war/WEB-INF/springapp-servlet.xml as defined in Developing a Spring Framework MVC application step-by-step

    case 1:
    Code:
        public ModelAndView onSubmit(Object command) throws ServletException { 
          ...
          // increase logic 
          return new ModelAndView(new RedirectView(getSuccessView()));
        }
    getSuccessView() returns hello.htm as defined here:
    Code:
        <bean id="priceIncreaseForm" class="web.PriceIncreaseFormController">
          <property name="sessionForm"><value>true</value></property>
          <property name="commandName"><value>priceIncrease</value></property>
          <property name="commandClass"><value>bus.PriceIncrease</value></property>
          <property name="validator"><ref bean="priceIncreaseValidator"/></property>
          <property name="formView"><value>priceincrease</value></property>
          <property name="successView"><value>hello.htm</value></property>
          <property name="productManager">
            <ref bean="prodMan"/>
          </property>
        </bean>
    hello.htm will then be wrapped into a RedirectView. This will result in a javax.servlet.http.HttpServletResponse.sendRedirec t to /hello.htm send to the Web Agent (IE, Mozialla, ...).

    Spring DispatcherServlet will then handle the new HTTP /hello.htm request and call the corresponding controller (SpringappController):
    Code:
        <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
          <property name="mappings">
            <props>
              <prop key="/hello.htm">springappController</prop>
              <prop key="/priceincrease.htm">priceIncreaseForm</prop>
            </props>
          </property>
        </bean>
    SpringappControler will return a new ModelAndView (hello):
    Code:
        public ModelAndView handleRequest&#40;HttpServletRequest request, HttpServletResponse response&#41; throws ServletException, IOException &#123;
          String now = &#40;new java.util.Date&#40;&#41;&#41;.toString&#40;&#41;;
          logger.info&#40;"returning hello view with " + now&#41;;
          Map myModel = new HashMap&#40;&#41;;
          myModel.put&#40;"now", now&#41;;
          myModel.put&#40;"products", getProductManager&#40;&#41;.getProducts&#40;&#41;&#41;;
    
          return new ModelAndView&#40;"hello", "model", myModel&#41;;
        &#125;
    Finally, the user will be redirected to /WEB-INF/jsp/hello.jsp as resolved by the viewResolver:
    Code:
        <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>
    case 2:
    Code:
        protected void doSubmitAction&#40;Object command&#41; throws Exception &#123;
          ...
          // increase logic 
          // no need for the return of ModelAndView&#40;...&#41; 
        &#125;
    Since no ModelAndView is configured in doSubmitAction, null (default value for successView) will be used, but this is not a sendRedirect!!!.

    hello.html (configured value for successView) will be resolved immediately by the viewResolver and the user will be redirected to /WEB-INF/jsp/hello.htm.jsp witch does not exist.

    I hope this will help.

    Comment


    • #3
      If I may add, this is why it's not an incredibly great idea for the controller to know that some view names are meant to be used as is (new ModelAndView(name... ), and some as a RedirectView, where the name is an actual html page (new ModelAndView(new RedirectView(name ... ). It's just not very clean and leads to these sorts of things. I will actually be adding some code to the view handlers I think, so that view names can be decorated, and view resolvers which can't be chained currently (InternalResourceViewResolver, etc.) can be chained and pick a target view type based on some criteria like a regex.

      Regards,

      Comment

      Working...
      X