![]() |
thirdparty image |
Execution flow:-
- Client sends an HTTP request to a specific URL
- DispatcherServlet of Spring MVC receives the request
- It passes the request to a specific controller depending on the URL requested using @Controller and @RequestMapping annotations.
- Spring MVC Controller then returns a logical view name and model to DispatcherServlet.
- DispatcherServlet consults view resolvers until actual View is determined to render the output
- DispatcherServlet contacts the chosen view (e.g. Freemarker, JSP) with model data and it renders the output depending on the model data
- The rendered output is returned to the client as response
ü You should Configure DispatcherServlet
in web.xml as following..
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
ü At web container startup.
·
DispatcherServlet
will be loaded , Instantiated and initialized by calling init() method.
·
Init()
method of DispatcherServlet will try to identify the Spring Configuration
Document with following naming conversions.
Servlet_name-servlet.xml
Ex:
Dashzin-servlet.xml
ü DispatcherServlet create the ApplicationContext (XmlWebApplicationContext) object by reading all the beans specified in the Spring Configuration Document.
ü If Spring Configuration Document is
not found with the given naming conversion then ApplicationContext object will
not be created by the DispatcherServlet.
Example:-
Public
class DispatcherServlet extends HttpServlet{
ApplicationContext ctx=null;
Public
void init(ServletConfig cfg){
//
try to get the spring Configuration
Documents with the default naming conversions
String
xml=”Dashzin”+”-servlet.xml”;
//
if found then create the ApplicationContext object
Ctx=new
XmlWebApplicationContext(xml);
}
……
}
After Deploying and starting SpringMVC based web application , following tasks will happen at web Container start-up.
ü Web Container loads, create and initialize
DispatcherServlet by calling init() method.
ü DispatcherServlet’s init() performs
the following:-
·
Identifies
spring configuration document file
·
Spring
Container instance will be created by reading all the beans from identified
spring configuration document
·
Initializes
the DispatcherServlet with spring container instances
Following task will happen at Web Container shutdown-time
ü Web container call destroy() method
of DispatcherServlet.
ü destroy() method of DispatcherServlet
destroys the spring container instance.