View Full Version : PriceIncreaseFormController from the MVC tutorial in the doc
sprior
08-12-2004, 12:26 PM
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?
irbouho
08-13-2004, 03:23 PM
sprior,
In this post I will refer to springapp/war/WEB-INF/springapp-servlet.xml (http://www.springframework.org/docs/MVC-step-by-step/Spring-MVC-step-by-step-Part-4.html) as defined in Developing a Spring Framework MVC application step-by-step (http://www.springframework.org/docs/MVC-step-by-step/Spring-MVC-step-by-step.html)
case 1:
public ModelAndView onSubmit(Object command) throws ServletException {
...
// increase logic
return new ModelAndView(new RedirectView(getSuccessView()));
}
getSuccessView() returns hello.htm as defined here:
<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):
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlH andlerMapping">
<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):
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String now = (new java.util.Date()).toString();
logger.info("returning hello view with " + now);
Map myModel = new HashMap();
myModel.put("now", now);
myModel.put("products", getProductManager().getProducts());
return new ModelAndView("hello", "model", myModel);
}
Finally, the user will be redirected to /WEB-INF/jsp/hello.jsp as resolved by the viewResolver:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResou rceViewResolver">
<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:
protected void doSubmitAction(Object command) throws Exception {
...
// increase logic
// no need for the return of ModelAndView(...)
}
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.
Colin Sampaleanu
08-13-2004, 04:20 PM
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,
vBulletin® v3.7.0, Copyright ©2000-2008, Jelsoft Enterprises Ltd.