The first thing that we do when we want to implement Spring MVC in our project is to add DispatcherServlets entry in deployment descriptor (web.xml). Also we write a spring web configuration xxxx-servlet.xml which contains all the MVC mappings and data.
By default the name of file must be XXX-servlet.xml where XXX is the name of servlet. For example in below entry in Web.xml we defined servlet named “spring”.
<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>
Code language: HTML, XML (xml)
Note that the servlet name is “spring” and thus, Spring will by default load file named “spring-servlet.xml” from your webapps WEB-INF folder.
Well, this can be achieved by passing one init-parameter to spring’s DispatcherServlet. Check the following web.xml snippet.
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/bean.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
Code language: HTML, XML (xml)
Note that in above code snippet, we have passed an init-param to DispatcherServlet called contextConfigLocation. Using this parameter not only can we change the name of Spring’s web context file but also change its location.
This parameter will call setContextConfigLocation
method on DispatcherServlet
and overrides default context config file. Note that it is possible to add multiple locations separated by any number of commas and spaced.
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/bean.xml, /WEB-INF/bean-service.xml, /WEB-INF/bean-dao.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
Code language: HTML, XML (xml)
I hope this is useful :)
Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
its very good to know this new feature of spring ...
is it available in all spring version ?
thankx i was searching for this
That's right. I found out that you can use the ContextLoaderListener-class to include multiple application also, e.g. like this
contextConfigLocation
classpath:applicationContext-persistence-relational.xml
org.springframework.web.context.ContextLoaderListener
Matt
Not very helpful 2 me...
the data is not there whatever i m looking 4.
Can you configure multiple init params in DispatcherServlet ?
flexspring
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
spring.profiles.active
production
1
Very good explanation ever seen this information thank you very much
Good and basic thing one should know to start with Spring.
Yeah.. Its very useful....and.basic thing one should know to start with Spring.
Excellent tutorial. Worked fine.
i have problem like............ this above site is online now.........but in hierarchy....... /first.jsp and session.jsp set the session variable and /Temp/second.jsp get the session value............but if i place a session.jsp in main ROOT dir the it gives me value...............but in /Temp/session.jsp............it gives me NULL pointer exception..........................
applcation.setAttribute("name1",name) // session.jsp
application.getAttribute("name1") // /Temp/second.jsp.................. NULL...........................
plz help me out......... thanks in advance