Configuring of Spring MVC by R4R Team

Configuring of Spring MVC:-

We need to map the requests that you want the DispatcherServlet to handle, by using a URL mapping in the web.xml file. The following is an example to show declaration and mapping for DispatcherServlet. DispatcherServlet is a normal servlet class which implements HttpServlet base class. Thus we need to configure it in web.xml.

<web-app>

<servlet>

<servlet-name>exampleservlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>

<load-on-startup>1</load-on-startup>

servlet>

<servlet-mapping>

<servlet-name>exampleservlet-name>

<url-pattern>*.jspurl-pattern>

servlet-mapping>

web-app>

In above code snippet, we have configure DispatcherServlet in web.xml. Note that we have mapped *.jsp url pattern with example DispatcherServlet. Thus any url with *.jsp pattern will call Spring MVC Front controller.

 

Note the above architecture diagram. The WebApplicationContext specified in above diagram is an extension of the plain ApplicationContext with some extra feature necessary for web applications. The WebApplicationContext is capable of resolving themes and it is also associated with corresponding servlet.

The web.xml file will be kept WebContent/WEB-INF directory of your web application. OK, upon initialization of DispatcherServlet, the framework will try to load the application context from a file named [servlet-name]-servlet.xml located in the application's WebContent/WEB-INF directory.

If you do not want to go with default filename as [servlet-name]-servlet.xml and default location as WebContent/WEB-INF, you can customize this file name and location by adding the servlet listener ContextLoaderListener in your web.xml file as follows:

<web-app...>

<context-param>

<param-name>contextConfigLocationparam-name>

<param-value>/WEB-INF/Example-servlet.xmlparam-value>

context-param>

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

listener-class>

listener>

web-app>

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!