Request life cycle of Spring web applications

This post will analyse the request life cycle of Spring web applications with the codes of Spring Framework MVC application I did in lab.

1. Browser sends request, received by DispatcherServlet.

DispatcherServlet is set in war/WEB-INF/web.xml.

<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>

All the reqests as the name of *.htm will be handled by springapp, which is a DispatcherServlet.

2. To figure which controller to use.

<bean id=”urlMapping”>
<property name=”mappings”>
<props>
<prop key=”/hello.htm”>springappController</prop>
<prop key=”/priceincrease.htm”>priceIncreaseForm</prop>
</props>
</property>
</bean>

This codes in war/WEB-INF/springapp-servlet.xml is mapping /hello.htm and /priceincrease.htm to 2 controllers separately.

3. Dispatcher server requests Controller, controller returns ModelAndView object.

In src/web/SpringappController.java

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);
}

The controller returns a ModelAndView object whose viewName is “hello”, modelName is “model” and model object is a Map object named as myModel.

4. If ModelAndView contains logical name for a View, DispatcherServlet queries ViewResolver for the View object that will render the response.

<bean id=”viewResolver”>
<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>

This codes will add prfix and suffix to the viewName of ModelAndView object, in this example, hello will be fixed as /WEB-INF/jsp/hello.jsp .

5. DispatcherServlet requests the View Object. /WEB-INF/jsp/hello.jsp will be shown on browser.

Advertisements

About Sanju
I am Software Programmer. I am working in JAVA/J2EE Technologies.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: